JDBC
创建以及插入数据
JDBC API允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据。
执行步骤:
- 联接数据源,如:数据库。
- 为数据库传递查询和更新指令。
- 处理数据库响应并返回处理的结果。
JDBC编程的步骤:
加载驱动程序:
1 2 3
| Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/DATABASE", "UESRNAME", "PASSWORD");
|
创建Statement\PreparedStatement对象:
conn.createStatement(); //创建sql对象
conn.prepareStatement(sql); //预编译sql
实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| >import java.sql.*; >import java.sql.DriverManager; >import java.sql.Statement; >public static void main(String[] args) throws ClassNotFoundException { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/learn", "root", "password"); Statement stmt=conn.createStatement(); String q="insert into jdbc (num,id) values(?,?)"; PreparedStatement t= conn.prepareStatement(q); t.setInt(1,2); t.setString(2,"rng"); t.execute(); String s="insert into jdbc (num,id) values(1,'we')"; stmt.execute(s); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e) { System.out.println("wrong!"); }catch(Exception e) { e.printStackTrace(); }
>}
|
其中的赋值操作t.setInt(1,2);t.setString(..)等等都可以写为:t.setObject(1,2)……;
下面是jdbc操作与student类的综合使用的实例:
建立连接:MysqlDemo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import java.sql.Connection; import java.sql.DriverManager; import java.sql.*;
public class MysqlDemo { private static Connection conn=null; static { try{ System.out.println("开始!"); Class.forName("com.mysql.cj.jdbc.Driver"); conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","password"); }catch(ClassNotFoundException e) { e.printStackTrace(); }catch(SQLException e) { System.out.println("Wrong!"); }catch(Exception e) { e.printStackTrace(); } } public static Connection getConnection() { return conn; } }
|
Student类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class Student { private String name; private int id; private String address; public Student(int id,String name,String address) { this.id=id; this.name=name; this.address=address; } public String toString() { return "id: "+id+" name: "+name+" address: "+address; } public int getId() { return id; } public String getName() { return name; } public String getAddress() { return address; }
}
|
添加数据,Test类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import javax.swing.plaf.nimbus.State; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.util.*;
public class Test { public static void main(String []args) throws SQLException { Connection conn=MysqlDemo.getConnection(); System.out.println("请输入学生人数:"); int num; Scanner input=new Scanner(System.in); num=input.nextInt(); Student st[]=new Student[num]; int i=0; String s="truncate table student"; Statement q=conn.createStatement(); q.execute(s); while(num!=0) { num--; String sql="insert into student (id,name,address)values(?,?,?) "; System.out.println("请输入第"+i+"名学生信息: "); int id=input.nextInt(); String name=input.next(); String address=input.next(); st[i]= new Student(id,name,address); PreparedStatement ptmt=conn.prepareStatement(sql); ptmt.setInt(1,st[i].getId()); ptmt.setString(2,st[i].getName()); ptmt.setString(3,st[i].getAddress()); ptmt.execute(); i++; } } }
|
将jdbc应用于面向对象时要注意封装的问题,一般驱动连接的构造最好使用静态初始化块,变量以及方法因而同样也要声明为静态方法以及参数。
查询数据
下面用两个例子来说明在createStatement和PrepareStatememnt两种有参以及无参的情况:
无参:
1 2 3 4 5 6 7 8 9 10
| String sql="select *from student"; Statement s=conn.createStatement(); ResultSet re=s.executeQuery(sql); while(re.next()) { int id=re.getInt("id"); String address=re.getString("address"); String name=re.getString("name"); System.out.println(name+" "+id+" "+address); }
|
有参:
1 2 3 4 5 6 7 8 9 10 11
| String sql="select *from student where id=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setInt(1,1); ResultSet re=pst.executeQuery(); while(re.next()) { int id=re.getInt("id"); String address=re.getString("address"); String name=re.getString("name"); System.out.println(name+" "+id+" "+address); }
|