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文件包
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值