![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLzEERNRTW61UNNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2MzN4EjNzEjM3AjMxgTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
测试与数据库建立连接
package cn.dym01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 测试与数据库建立连接
* @author Administrator
*
*/
public class Demo01 {
public static void main(String[] args) {
try {
// 加载驱动类
Class.forName("com.mysql.jdbc.Driver");
long start=System.currentTimeMillis();
//建立连接(连接对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是Connection对象管理的一个要点!)
//真正开发中,为了提高效率,都会使用 连接池 来管理连接对象
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
long end=System.currentTimeMillis();
System.out.println(conn);
System.out.println("建立连接,耗时:"+(end-start)+"ms");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试执行SQL语句,以及SQL注入问题 所以一般不用statement
package cn.dym01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 测试执行SQL语句,以及SQL注入问题 所以一般不用statement
* @author Administrator
*
*/
public class Demo02 {
public static void main(String[] args) {
try {
// 加载驱动类
Class.forName("com.mysql.jdbc.Driver");
//建立连接(连接对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是Connection对象管理的一个要点!)
//真正开发中,为了提高效率,都会使用 连接池 来管理连接对象
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
Statement stmt= conn.createStatement();
String sql="insert into t_user (username,pwd,regTime) values ('苍凌','cl',NOW());";
stmt.execute(sql);
//测试SQL注入
String id="5 or 1=1"; //此句一执行,该数据库的数据表中的数据将会被清空不见
sql="delete from t_user where id="+id;
stmt.execute(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试 PreparedStatement的基本用法
package cn.dym01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 测试 PreparedStatement的基本用法
* @author Administrator
*
*/
public class Demo03 {
public static void main(String[] args) {
try {
// 加载驱动类
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
String sql="insert into t_user (username,pwd,regTime) values (?,?,?)"; // ? 代表占位符
PreparedStatement ps=conn.prepareStatement(sql);
// ps.setString(1, "代止兮"); // 参数索引是从1开始计算,而不是0
// ps.setString(2, "14");
// ps.setDate(3, new java.sql.Date(System.currentTimeMillis()));
//可以使用setObject方法处理参数,
ps.setObject(1, "苍凌uyk");
ps.setObject(2, "cljh");
ps.setObject(3, new java.sql.Date(System.currentTimeMillis()));
System.out.println("插入一行记录");
ps.execute();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试ResultSet结果集的基本用法
package cn.dym01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 测试ResultSet结果集的基本用法
*
*/
public class Demo04 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","123456");
String sql = "select id,username,pwd from t_user where id>?"; //?占位符
ps = conn.prepareStatement(sql);
ps.setObject(1, 13); //把id大于13的记录都取出来
rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+"---"+rs.getString(2)+"---"+rs.getString(3));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
//遵循:resultset-->statment-->connection这样的关闭顺序!一定要将三个trycatch块,分开写!
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package cn.dym01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 测试批处理的基本用法
*
*/
public class Demo05 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","123456");
conn.setAutoCommit(false); //设为手动提交
long start = System.currentTimeMillis();
stmt = conn.createStatement();
for(int i=0;i<20000;i++){
stmt.addBatch("insert into t_user (username,pwd,regTime) values ('代"+i+"',666666,now())");
}
stmt.executeBatch();
conn.commit(); //提交事务
long end = System.currentTimeMillis();
System.out.println("插入20000条数据,耗时(毫秒):"+(end-start));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
//遵循:resultset-->statment-->connection这样的关闭顺序!一定要将三个trycatch块,分开写!
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(stmt!=null){
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}