java中Jdbc最基础的使用

一、注册Driver

//注册驱动的两种方式
try{
	//第一种
	Class.forName(driver);
	
	//第二种
	DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
}catch(Exception e){
	e.printStackTrace();
}

二、配置数据库连接信息

//第一种:直接在java代码中配置数据库连接信息
String url = "jdbc:mysql://localhost:3306/databaseName";
String username = "root";
String password = "123456";

//第二种:创建配置文件,在Java代码中引用配置文件中的信息
ResourceBundle bundle = ResourceBundle.getBundle("config");
String driver = bundle.getString("driver");
String user = bundle.getString("user");
String password = bundle.getString("password");
String url = bundle.getString("url");

//配置文件config.properpies中的代码
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/databaseName
user=root
password=123456

三、获取数据库操作对象和数据库语句操作对象

try{
	Statement stmt = null;
	Connection conn = null;
	conn=DriverManager.getConnection(url,user,password);
	stmt = conn.createStatement();
}catch(Exception e){
	System.out.print("获取操作对象失败!");
}

四、执行SQL语句

try{
	//定义SQL语句
	String sql = "insert into t_test(name,sex,age) values('dai',2,12)";
	//获取数据库返回信息
	int count=stmt.executeUpdate(sql);
	//打印提示信息
	System.out.println(count==1 ? "插入数据成功!": "插入失败!");
}catch(Exception e){
	System.out.print("SQL语句执行失败!");
}

五、关闭操作对象

finally {
          if(stmt!=null){
                 try{
                     stmt.close();
                }catch(Exception e){
                     e.printStackTrace();
               		 }
                }
          if(conn!=null){
                 try{
                     conn.close();
                }catch(Exception e){
                     e.printStackTrace();
                    }
                }
         }

Test

一、获取数据库中的数据并打印

public static void main(String[] args) {
        Statement stmt = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            ResourceBundle bundle = ResourceBundle.getBundle("config");
            String driver = bundle.getString("driver");
            String user = bundle.getString("user");
            String password = bundle.getString("password");
            String url = bundle.getString("url");
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);
            stmt = conn.createStatement();
            String sql = "select * from t_student;";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                System.out.println(rs.getString("id") + "-"
                        + rs.getString("name") + "-"
                        + rs.getString("age") + "-"
                        + rs.getString("sex") + "-"
                        + rs.getString("phone") + "-"
                        + rs.getString("address")
                );
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

Mysql_JDBC_connect.jar文件包

一、Mysql_JDBC_connect.jar文件包

上一篇:使用Idea连接数据库(事务,数据库连接池)


下一篇:go 连接 mysql: could not use requested auth plugin ‘mysql_native_password‘