• JDBC 第一个程序


    # JJU_干干

    数据库加载:

    // 加载,固定的语句
                Class.forName("com.mysql.jdbc.Driver");

    数据库连接:conn = DriverManager.getConnection(url, user, password);

    (1) url 中 test 是数据库名称

    //数据库连接
    String url = "jdbc:mysql://127.0.0.1:3306/test";
    String user = "root";
    String password = "zzgzzg";
    conn = DriverManager.getConnection(url, user, password);

    数据库访问并获取结果:state = conn.createStatement();   rs = state.executeQuery(sql);

    // 数据库访问并获取结果
    state = conn.createStatement();
    // sql 语句
    String sql = "select id,name,sex,birthday from student";
    rs = state.executeQuery(sql);

    循环获取字段数据并输出:

    (1) 获取字段:rs.getInt(), rs.getString() 

    (2) 指针到下一行:rs.next()

    // 循环获取字段数据并输出
                while(rs.next()) {
                    int id = rs.getInt(1); // 获取第一个字段,id
                    String name = rs.getString("name"); // 获取名字
                    String sex = rs.getString("sex");
                    Date birthday = rs.getDate("birthday");
                    
                    // 表内容
                    out.print("<tr>"
                            +"<td>"+id+"</td>"
                            +"<td>"+name+"</td>"
                            +"<td>"+sex+"</td>"
                            +"<td>"+birthday+"</td>"
                            + "</tr>");
                }

    代码

    package case01;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Date;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    @WebServlet("/ListStu")
    public class ListStu extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        
        public ListStu() {
            super();
            
        }
    
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset = utf-8");
            PrintWriter out = response.getWriter();
            
            Connection conn = null;
            Statement state = null;
            ResultSet rs = null;
            
            // mysql 数据库驱动程序的加载
            try {
                // 固定的语句
                Class.forName("com.mysql.jdbc.Driver");
                out.print("数据库驱动程序加载成功!");
                
                //数据库连接
                String url = "jdbc:mysql://127.0.0.1:3306/test";
                String user = "root";
                String password = "zzgzzg";
                conn = DriverManager.getConnection(url, user, password);
                out.print("<br>"+"数据库连接成功!");
                
                // 数据库访问
                state = conn.createStatement();
                String sql = "select id,name,sex,birthday from student";
                rs = state.executeQuery(sql);
                
                // 画表
                out.print("<table border=1>");
                // 表头
                out.print("<tr>"
                        +"<td>id</td>"
                        +"<td>name</td>"
                        +"<td>sex</td>"
                        +"<td>birthday</td>"
                        + "</tr>");
                
                while(rs.next()) {
                    int id = rs.getInt(1); // 获取第一个字段,id
                    String name = rs.getString("name"); // 获取名字
                    String sex = rs.getString("sex");
                    Date birthday = rs.getDate("birthday");
                    
                    // 表内容
                    out.print("<tr>"
                            +"<td>"+id+"</td>"
                            +"<td>"+name+"</td>"
                            +"<td>"+sex+"</td>"
                            +"<td>"+birthday+"</td>"
                            + "</tr>");
                }
                
                out.print("</table>");
                out.print("<br>"+"数据库访问成功!");
                
            } catch (ClassNotFoundException e) {
                
                e.printStackTrace();
                out.print("数据库驱动程序加载失败!");
                
            } catch (SQLException e) {
                
                e.printStackTrace();
                out.print("数据库连接失败!");
            }finally {
                // 回收资源
                // 回收 conn
                if(conn!=null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    conn = null;
                }
                // 回收state
                if(state!=null) {
                    try {
                        state.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    state = null;
                }
                // 回收 rs
                if(rs!=null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    rs = null;
                }
                
            }
                 
        }
    
        
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            doGet(request, response);
        }
    
    }

    运行结果:

  • 相关阅读:
    android学习---ListView
    android学习---ViewStub惰性装载控件
    android学习---TabHost
    微信小程序二维码带参数
    as
    niginx高性能原因
    Nginx动态反向代理1
    Niginx反向代理负载均衡
    jmeter测试,定制化内嵌tomcat调优
    Git通过密钥对远程仓库上传和更新详细操作
  • 原文地址:https://www.cnblogs.com/ZZG-GANGAN/p/13953017.html
Copyright © 2020-2023  润新知