使用jdbc连接数据库需要两个步骤 1、加载驱动类 需要导入mysql连接jar包 https://dev.mysql.com/downloads/connector/j/ Class.forName(com.mysql.jdbc.Driver); 2、得到连接对象 Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydb"+"?serverTimezone=GMT%2B8","root","123456"); 其中连接数据库是经常回抛出以下异常: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. 解决方法: 在url中添加+"?serverTimezone=GMT%2B8" 原因:这个表示系统时区的错误,修正时区值就可以了
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Diver
解决方法:1、查看是否有导入jar包 2、检查驱动名是否有打错
java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)
解决方法:检查用户名和密码输入是否有错误并修改
@Test public void fun1() throws ClassNotFoundException, SQLException{ /* * jdbc四大配置参数 * driverClassName:com.mysql.jdbc.Driver * url:jdbc:mysql://localhost:3306/数据库名 * username * password * */
Class.forName("com.mysql.cj.jdbc.Driver");
//String url="jdbc:mysql://localhost:3306/mydb"+"?serverTimezone=GMT%2B8";
//String username="root";
//String password="123456";
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb"+"?serverTimezone=GMT%2B8","root","123456");
System.out.println(con);
}
** 进行增删改查** /* * 1、对数据库做增、删、改 * 1.通过Connection对象创建Statement * Statement语句的发送器,它的功能就是向数据库发送sql语句 * 2、调用它的 int executeUpdate(String sql),它可以发送DML,DDL * * */
//1、通过Connection得到Statement对象
Statement stmt=con.createStatement( );
//使用statement发送sql语句
//String sql=("INSERT INTO teacher VALUES('5','XDD')");
String sql=("UPDATE teacher SET tname='X老师' WHERE tid='5'");
int r=stmt.executeUpdate(sql);
System.out.println(r);
查询 //配置四大参数 Class.forName("com.mysql.cj.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/mydb"+"?serverTimezone=GMT%2B8"; String username="root"; String password="123456";
Connection con=DriverManager.getConnection(url,username,password);
/*
* 得到Statement,执行select语句
* 1、得到Statement对象:Connection的createStatement()方法
*
* */
Statement stmt=con.createStatement();
//2、调用Statement的ResultSet rs=executeQuery(String querySql)专门用来查询的方法
ResultSet rs=stmt.executeQuery("select * from teacher");
/*
* 3、解析ResultSet
* 把光标移动到第一行,可以调用next()方法完成
* */
//rs.next()光标往下进行
while(rs.next()){
int tid=rs.getInt(1);//表的第一列
String tname=rs.getString("tname");//名为tname的那一列
System.out.println(tid+","+tname);
}
//4、关闭资源,倒关
rs.close();
stmt.close();
con.close();
}
获取结果集元数据
得到元数据:rs.getMetaData(),返回值为ResultSetMetaData;
获取结果集列数:int getColumnCount()
获取指定列的列名:String getColumnName(int collndex)
PreparedStatement 它是Statement接口的子接口 强大之处: 防止sql攻击; 提高代码可读性、维护性 提高效率 学习PreparedStatement 的用法 如何得到PreparedStatement对象 给出sql模板 调用Connection的PreparedStatement preparedStatement(String sql模板); 调用pstmt的setxxx()系列方法sql模板中的?赋值 调用pstmt的executeUpdate()或executeQuery(),但它的方法都没有参数 //获取Statement //Statement stmt=con.createStatement();
//得到ResultSet
//String sql="select *from t_user where username='"+username+"'and pwd='"+pwd+"'";
//ResultSet rs=stmt.executeQuery(sql);
//return rs.next();
以上为Statement的使用方法
//
/*
* 1、得到PreparedStatement
* 给出sql模板:所有的参数使用?来替代
* 调用Connection方法,得到PreparedStatement
* */
String sql="select *from t_user where username=?and pwd=?";
PreparedStatement pstmt=con.prepareStatement(sql);
/*
* 2、为参数赋值
* */
pstmt.setString(1, username);//给第一个问号赋值,值为username
pstmt.setString(2, pwd);//给第二个问号赋值,值为pwd
ResultSet rs=pstmt.executeQuery();
return rs.next();
批处理 /* * pstmt: * 添加参数到批中 * 执行批 * */ Connection con=JdbcUtils.getConnection(); String sql="INSERT INTO t_user VALUES(?,?)"; PreparedStatement pstmt=con.prepareStatement(sql);
//疯狂的添加参数
for(int i=0;i<1000;i++){
pstmt.setInt(1, i+1);
pstmt.setString(2, "stu_"+i);
pstmt.addBatch();//添加批,将参数保存到集合中
}
long start=System.currentTimeMillis();
pstmt.executeBatch();//执行批
long end=System.currentTimeMillis();
System.out.println(end-start);
效果