连接MySql数据库和查询数据

1、在项目下的src路径下进行创建JavaBean,用于连接数据库。
package com.bat;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DataHe {
    String driver= "com.mysql.jdbc.Driver";
    String url= "jdbc:mysql://localhost:3306/hu";
    String user="root";
    String password="qwe123";
    
    public Connection getConn(){ 
        Connection conn=null;
        try {
               //1、加载驱动
            Class.forName(driver);
             //2、创建连接
             conn = DriverManager.getConnection(url, user, password);
            
        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          return conn;  
    }
    
    
    public void  closeAl(ResultSet rs,Statement stmt,Connection conn){
        
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(stmt!=null){
          try {
            stmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    } 
}
2、在创建jsp页面进行渲染数据库中的数据(进行查询操作)。
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
    
    
        
    
    
    
  
  
  
 
    
     <% 
     try{
     Connection con=null;
     String sql="select * from student";
     DataHe data =new  DataHe();
      
     con=data.getConn();
    Statement stmt=con.createStatement();
    ResultSet rs=stmt.executeQuery(sql);  
    while(rs.next()){
  %>
    
The order number Student name Student age Student deploy telephone qq'number
<%=rs.getInt("id") %> <%=rs.getString("stu_name") %> <%=rs.getString("stu_age") %> <%=rs.getString("stu_deploy") %> <%=rs.getString("telephone") %> <%=rs.getString("qq") %>
<% data.closeAl(rs, stmt, con); } } catch(Exception e){System.out.print("出现错误");} %>
3、下面的这个代码是直接单独用jsp界面实现的查询数据库数据的功能。



  
    My JSP 'test.jsp' starting page

    
    
        
    
    
    

  
  
  
  <%


String driver = "com.mysql.jdbc.Driver";

// URL指向要访问的数据库名test1

String url = "jdbc:mysql://127.0.0.1:3306/hu";

// MySQL配置时的用户名

String user = "root";

// Java连接MySQL配置时的密码

String password = "qwe123";

try {

// 1 加载驱动程序

Class.forName(driver);

// 2 连接数据库

Connection conn = DriverManager.getConnection(url, user, password);

// 3 用来执行SQL语句

Statement statement = conn.createStatement();

// 要执行的SQL语句

String sql = "select * from student";

ResultSet rs = statement.executeQuery(sql);
String name = null;
String mima=null;
while (rs.next()) {
name = rs.getString("stu_age");
mima = rs.getString("stu_name");
out.println(name+"\t"+mima);
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

%>



你可能感兴趣的:(连接MySql数据库和查询数据)