一、JDBC
JDBC:Java DateBase Connective 是一组专门用于java连接数据库的标准,在整个JDBC当中,提供了大量的接口,针对各种不同的数据库,想要使用java进行连接,只需要找到对各个数据库标准的支持就行了。
二、JDBC的操作步骤
·加载驱动程序
·通过Connection和DriverManager完成数据库的连接
·通过一些类完成数据库的操作
·Statement:完成数据库的增删改查操作(也叫数据库操作类)
·PreparedStatement:完成数据库的增删改查操作,但是多了一个预处理的过程
·ResultSet:接受数据库查询结果
·关闭数据库连接,在实际开发中,数据库的资源非常有限,操作完成之后必须要关闭,不然很容易照成数据库的崩溃。
package JDBCDemo;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
public class Demo01 {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://127.0.0.1:3306/boc";
public static final String USERNAME="root";
public static final String PASSWORD="boc";
public static void main(String[] args) {
Connection conn =null;
try {
Class.forName(DRIVER);
conn=(Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
System.out.println(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//加载驱动
}
}
(一)插入操作
package JDBCDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
package JDBCDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo01 {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://127.0.0.1:3306/boc";
public static final String USERNAME="root";
public static final String PASSWORD="boc";
public static void main(String[] args) {
Connection conn =null;
Statement stat = null;
try {
Class.forName(DRIVER);//加载驱动
conn= DriverManager.getConnection(URL,USERNAME,PASSWORD);//获得数据库连接
stat = conn.createStatement();//通过连接对象完成statement操作类的实例化
String sql = "insert into student (s_name,s_age,s_sex) values ('Sam',23,0) ";
stat.execute(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
stat.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
(二)删除操作
package JDBCDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo01 {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://127.0.0.1:3306/boc";
public static final String USERNAME="root";
public static final String PASSWORD="boc";
public static void main(String[] args) {
Connection conn =null;
Statement stat = null;
try {
Class.forName(DRIVER);//加载驱动
conn= DriverManager.getConnection(URL,USERNAME,PASSWORD);//获得数据库连接
stat = conn.createStatement();//通过连接对象完成statement操作类的实例化
String sql = "delete from student where s_name='Sam' ";
stat.execute(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
stat.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
(三)修改操作
package JDBCDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo01 {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://127.0.0.1:3306/boc";
public static final String USERNAME="root";
public static final String PASSWORD="boc";
public static void main(String[] args) {
Connection conn =null;
Statement stat = null;
try {
Class.forName(DRIVER);//加载驱动
conn= DriverManager.getConnection(URL,USERNAME,PASSWORD);//获得数据库连接
stat = conn.createStatement();//通过连接对象完成statement操作类的实例化
String sql = "update student set s_name='Sam',s_age=55 where s_id=1 ";
stat.execute(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
stat.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
(四)查找操作
package JDBCDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Demo01 {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://127.0.0.1:3306/boc";
public static final String USERNAME="root";
public static final String PASSWORD="boc";
public static void main(String[] args) {
Connection conn =null;
Statement stat = null;
ResultSet rs = null;
List list = new ArrayList();
try {
Class.forName(DRIVER);//加载驱动
conn= DriverManager.getConnection(URL,USERNAME,PASSWORD);//获得数据库连接
stat = conn.createStatement();//通过连接对象完成statement操作类的实例化
String sql = "select s_id,s_name,s_age,s_sex from student";
rs = stat.executeQuery(sql);
while(rs.next()){
Student s = new Student();
s.setsId(rs.getInt(1));
s.setsName(rs.getString(2));
s.setsAge(rs.getInt(3));
s.setsSex(rs.getInt(4));
list.add(s);
}
Iterator it = list.iterator();
while(it.hasNext()){
Student s = it.next();
if(s.getsSex()==0){
System.out.println(s.getsName()+"\t"+s.getsAge()+"\t"+"男");
}else{
System.out.println(s.getsName()+"\t"+s.getsAge()+"\t"+"女");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
stat.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
三、预处理
首先先来写一个程序,假设今天来了一个外国人,名字Mr’Money
String sql = “insert into student (s_name,s_age,s_sex) values (‘Mr’Money’,30,0)”;
对于这条语句而言,存在一个很严重的问题,在java中””括起来的内容我们成为字符串,如果这个字符串里有三个以上的’,插入数据的时候,数据库就无法正确辨别准确的数据。那么此时我们就需要预处理。
预处理:如果是新增操作,我们首先在数据库里先执行该语句,但是数据为空,在之后再数据的设置。使用的是PreparedStatement这个接口。
public boolean doIns(Student s) {
boolean bool = false;
Connection conn = GetConnection.getConnection();
PreparedStatement pstat = null;
try {
String sql = "insert into student (s_name,s_age,s_sex) values (?,?,?)";
pstat= conn.prepareStatement(sql);
pstat.setString(1, s.getsName());
pstat.setInt(2, s.getsAge());
pstat.setInt(3, s.getsSex());
pstat.execute();
bool=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pstat.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bool;
}
四、批处理
批处理:一次性执行多条sql语句
public boolean doInsList(List list) {
boolean bool=false;
Connection conn = GetConnection.getConnection();
Statement stat=null;
String sql=null;
try {
stat = conn.createStatement();
for(Student s:list){
sql = "insert into student (s_name,s_age,s_sex) values ('"+s.getsName()+
"',"+s.getsAge()+","+s.getsSex()+")";
stat.addBatch(sql);
}
stat.executeBatch();//执行批处理
bool=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
stat.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bool;
}
以上是普通的批处理操作,仅仅是将几条sql放进一个Batch,在执行的时候顺序执行。
public boolean doInsList(List list) {
boolean bool=false;
Connection conn = GetConnection.getConnection();
PreparedStatement pstat=null;
String sql=null;
try {
sql = "insert into student (s_name,s_age,s_sex) values (?,?,?)";
pstat=conn.prepareStatement(sql);
for(Student s:list){
pstat.setString(1, s.getsName());
pstat.setInt(2, s.getsAge());
pstat.setInt(3, s.getsSex());
pstat.addBatch();
}
pstat.executeBatch();//执行批处理
bool=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pstat.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bool;
}
以上就是使用预处理进行批处理。