主要内容:

  •  JDBC连接数据库步骤。
  • 一个简单详细的查询数据的例子。
  • 封装连接数据库,释放数据库连接方法。
  • 实现查询,插入,删除,更新等十一个处理数据库信息的功能。(包括事务处理,批量更新等)
  • 把十一个功能都放在一起。
  • 安装下载的数据库驱动程序jar包,不同的数据库需要不同的驱动程序(这本该是第一步,但是由于属于安装类,所以我们放在最后)

一.JDBC连接数据库(编辑)步骤(主要有六个步骤)。

 1.注册驱动: Class.forName("com.mysql.jdbc.Driver");显示的加载到JVM中

 2.
取连接:(1) param1:  要连接数据库的url-----》 String
url="jdbc:mysql://localhost:3306/test?"+
"useUnicode=true&characterEncoding=UTF8";//防止乱码

param2:要连接数据库的用户名--》 String user="h4";

param3:要连接数据库的密码----》 String pass="111";

Connection
conn=DriverManager.getConnection(url,user,pass);//DriverManager下的方
法:getConnection(String url,String username,String password)

(2)接下来我们分析下url:"jdbc(这是协议以jdbc开头):mysql(这是子协议,数据库管理系统名称)://localhost(数据库来源地址):3306(目标端口)/test(要查询的表)?"

"useUnicode=true&characterEncoding=UTF8";添加这个是为了防止乱码,指定使用Unicode字符
集 ,且使用UTF-8来编辑。

3.创建一个Statement语句对象(主要三种方法): Statement stmt=conn.createStatement();//Connection接口下的方法:Statement createStatement()

PreparedStatement pstmt = conn.PreparedStatement() ;

CallableStatement cstmt =  conn.prepareCall("{CALL demoSp(? , ?)}") ;

下面我们来分析下他们:(1) Statement与 PreparedStatement对象的区别,后者可以动态设置查询参数

(2)设置参数的方法 PreparedStatement.setXXXX(parameterIndex,value),如果数据库参数类型是varchar 则用setString,如果参数类型是Integer 则用setInt

(3)CallableStatement.setXXXX(parameterIndex,value)   //按照参数的顺序设置value

CallableStatement.setXXXX(parameterName,value)  //按照参数的名字来设置value,这个名字是在定义存储过程的时候的形式参数的名字

(4)CallableStatement.registerOutParameter方法用于声明一个存储过程输出类型的参数,用以接收存储过程的输出值

4.执行SQL语句: ResultSet rs=stmt.executeQuery(Sql);除了查询语句是executeQuery();其他全部是executeUpdate();

Statement接口下的方法:

boolean     execute(String sql):执行SQL语句,如果返回值是结果集则为true,否则为false

ResultSet  executeQuery(String sql):执行SQL语句,返回值为ResultSet

int            executeUpdate(String sql):执行SQL语句,返回值为所影响的行数

5.处理结果集:ResultSet对象的getXxxx方法,取决于数据库中表的字段的类型,例如:varchar2  对应方法是getString ,如果是 integer 对应方法是getInt/getLong

While(rs.next()){

rs.getInt(columnIndex);  //通过列的序号来获取字段的值

rs.getString(columnName);//通过列的名字来获取字段的值

ResultSet接口下常见的方法:beforeFirst();将游标移动到ResultSet中第一条记录(的前面)

afterLast();将游标移动到ResultSet中最后一条记录(的后面)

absolute(intcolumn):将游标移动到相对于第一行的指定行,负数则为相对于最后一条记录

previous():将游标上移一行

next():将游标下移一行

ResultSet.TYPE_SCROLL_SENSITIVE          结果集可以滚动

ResultSet.CONCUR_READ_ONLY   结果集只读,不能修改

6.关闭资源: 操作完以后要关闭jdbc来释放jdbc资源。但是顺序要和你定义对象的时候相反,就像关门一样,从里面先关,一直往外关。

如下示例:

if(rs !=null){//1.关闭结果集
                                             try{
                                                    rs.close();
                                                } catch (SQLException e){
                                                     e.printStackTrace();
                                               }
                                           }
                                   if(stmt !=null){//2.关闭声明的对象
                                            try{
                                                  stmt.close();
                                              }catch(SQLException e){
                                                    e.printStackTrace();
                                                  }
                                           }
                                   if(conn !=null){//3.关闭连接 (记住一定要先关闭前面的1.2.然后在关闭连接)
                                           try{
                                                 conn.close();
                                           }catch(Exception e){
                                              e.printStackTrace();
                                             }
                                        }


二.下面是一个简单的(详细的)查询数据的例子。

  1. 1 package javacore1;//这是你建的一个包名。
  2. 2
  3. 3 import java.sql.CallableStatement;
  4. 4 import java.sql.Connection;
  5. 5 import java.sql.DriverManager;
  6. 6 import java.sql.PreparedStatement;
  7. 7 import java.sql.ResultSet; //左边这五个是你在写程序时要导入的包名,记住是要在java.sql下的包。
  8. 8 import java.sql.SQLException;
  9. 9 import java.sql.Statement;
  10. 10 public class jdbc { //定义一个类
  11. 11 public static void main(String[] args){ //主方法
  12. 12 try{
  13. 13 String driver="com.mysql.jdbc.Driver"; //1.定义驱动程序名为driver内容为com.mysql.jdbc.Driver
  14. 14 String url="jdbc:mysql://localhost:3306/test?" //2.定义url;jdbc是协议;mysql是子协议:表示数据库系统管理名称;localhost:3306是你数据库来源的地址和目标端口;test是我本人建的表位置所在处,你以你的为标准。
  15. 15 + "useUnicode=true&characterEncoding=UTF8"; //防止乱码;useUnicode=true表示使用Unicode字符集;characterEncoding=UTF8表示使用UTF-8来编辑的。
  16. 16 String user="h4"; //3.定义用户名,写你想要连接到的用户。
  17. 17 String pass="111"; //4.用户密码。
  18. 18 String querySql="select * from employees"; //5.你想要查找的表名。
  19. 19 Class.forName(driver); //6.注册驱动程序,用java.lang包下面的class类里面的Class.froName();方法 此处的driver就是1里面定义的driver,也可以 Class.forName("com.mysql.jdbc.Driver");
  20. 21 Connection conn=DriverManager.getConnection(url,user,pass);//7.获取数据库连接,使用java.sql里面的DriverManager的getConnectin(String url , String username ,String password )来完成
  21. 22 //括号里面的url,user,pass便是前面定义的2,3,4步骤内容;
  22. 23 Statement stmt=conn.createStatement(); //8.构造一个statement对象来执行sql语句:主要有Statement,PreparedStatement,CallableStatement三种实例来实现
  23. 24 // 三种实现方法分别为:Statement stmt = con.createStatement() ;
  24. 25 // PreparedStatement pstmt = conn.prepareStatement(sql) ;
  25. 26 // CallableStatement cstmt = conn.prepareCall("{CALL demoSp(? , ?)}") ;
  26. 27 ResultSet rs=stmt.executeQuery(querySql);//9.执行sql并返还结束 ;ResultSet executeQuery(String sqlString):用于返还一个结果集(ResultSet)对象。
  27. 28 while(rs.next()){ //10.遍历结果集
  28. 29 System.out.println("人员编号:"+rs.getString("employee_id")+"工资:"+rs.getString("salary")+"姓名:"+rs.getString("last_name"));//使用getString()方法获取你表里的资料名
  29. 30 }
  30. 31 if(rs !=null){//11.关闭记录集
  31. 32 try{
  32. 33 rs.close();
  33. 34 } catch (SQLException e){
  34. 35 e.printStackTrace();
  35. 36 }
  36. 37 }
  37. 38 if(stmt !=null){//12.关闭声明的对象
  38. 39 try{
  39. 40 stmt.close();
  40. 41 }catch(SQLException e){
  41. 42 e.printStackTrace();
  42. 43 }
  43. 44 }
  44. 45 if(conn !=null){//13.关闭连接 (记住一定要先关闭前面的11.12.然后在关闭连接,就像关门一样,先关里面的,最后关最外面的)
  45. 46 try{
  46. 47 conn.close();
  47. 48 }catch(SQLException e){
  48. 49 e.printStackTrace();
  49. 50 }
  50. 51 }
  51. 52 }catch(Exception e){
  52. 53 e.printStackTrace();
  53. 54 }
  54. 55 }
  55. 56 }

三.为了更加直观,我们把获取数据库连接和释放连接封装在方法里,以便于后面更好的操作。

  1. 1 package javacore1;
  2. 2
  3. 3 import java.sql.Connection;
  4. 4 import java.sql.DriverManager;
  5. 5
  6. 6 public class jdbc{
  7. 7 public static void main(String[] args){
  8. 8
  9. 9 Connection conn= getConnection("h4", "111"); //获取数据库连接
  10. 10
  11. 11 /*,,,,此处为方法名来获取连接,例如 query(conn),,,,,*/
  12. 12
  13. 13 releaseConnection(conn); // 释放数据库连接
  14. 14 }
  15. 15
  16. 16 /*,,,,,,,,,,此处构建一个你想要的功能的方法,,,,,,,,,,,,*/
  17. 17
  18. 18
  19. 19 //数据库连接
  20. 20 public static Connection getConnection(String user, String pass) {
  21. 21
  22. 22 Connection conn = null;//声明连接对象
  23. 23 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  24. 24 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  25. 25 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  26. 26 try {
  27. 27 Class.forName(driver);// 注册(加载)驱动程序
  28. 28 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  29. 29 } catch (Exception e) {
  30. 30 e.printStackTrace();
  31. 31 }
  32. 32 return conn;
  33. 33 }
  34. 34 // 释放数据库连接
  35. 35 public static void releaseConnection(Connection conn) {
  36. 36 try {
  37. 37 if (conn != null)
  38. 38 conn.close();
  39. 39 } catch (Exception e) {
  40. 40 e.printStackTrace();
  41. 41 }
  42. 42 }
  43. 43 }

四.实现查询、插入、删除、更新等十一个处理数据库信息的功能。

1.查询数据

  1. 1 package javacore1;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.DriverManager;
  4. 4 import java.sql.ResultSet;
  5. 5 import java.sql.SQLException;
  6. 6 import java.sql.Statement;
  7. 7 public class Query {
  8. 8 public static void main(String[] args) {
  9. 9 Connection conn = getConnection("h4", "111");// 获取数据库连接
  10. 10 query(conn); //方法名调用数据库连接
  11. 11 releaseConnection(conn);//释放数据库连接
  12. 12 }
  13. 13 //查询数据,定义的query方法
  14. 14 public static void query(Connection conn){
  15. 15 String Sql="select * from employees";
  16. 16 try{
  17. 17 Statement stmt=conn.createStatement(); //也可以使用PreparedStatement来做
  18. 18 ResultSet rs=stmt.executeQuery(Sql);//执行sql语句并返还结束
  19. 19  
  20. 20 while(rs.next()){//遍历结果集 ,向下一行
  21. 21 System.out.println("人员编号:"+rs.getString("employee_id")+"工资:"+rs.getString("salary"));
  22. 22 }
  23. 23 if(rs !=null){
  24. 24 try{
  25. 25 rs.close();
  26. 26 } catch (SQLException e){
  27. 27 e.printStackTrace();
  28. 28 }
  29. 29 }
  30. 30 if(stmt !=null){
  31. 31 try{
  32. 32 stmt.close();
  33. 33 }catch(SQLException e){
  34. 34 e.printStackTrace();
  35. 35 }
  36. 36 }
  37. 37 if(conn !=null){
  38. 38 try{
  39. 39 conn.close();
  40. 40 }catch(SQLException e){
  41. 41 e.printStackTrace();
  42. 42 }
  43. 43 }
  44. 44 }catch(Exception e){
  45. 45 e.printStackTrace();
  46. 46 }
  47. 47 }
  48. 48 //数据库连接
  49. 49 public static Connection getConnection(String user, String pass) {
  50. 50 Connection conn = null;//声明连接对象
  51. 51 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  52. 52 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  53. 53 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  54. 54 try {
  55. 55 Class.forName(driver);// 注册(加载)驱动程序
  56. 56 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  57. 57 } catch (Exception e) {
  58. 58 e.printStackTrace();
  59. 59 }
  60. 60 return conn;
  61. 61 }
  62. 62 //释放数据库连接
  63. 63 public static void releaseConnection(Connection conn) {
  64. 64 try {
  65. 65 if (conn != null)
  66. 66 conn.close();
  67. 67 } catch (Exception e) {
  68. 68 e.printStackTrace();
  69. 69 }
  70. 70 }
  71. 71 }
  1. //查询数据,结果倒着显示,倒序
  2. package javacore1;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. public class Query {
  9. public static void main(String[] args) {
  10. Connection conn = getConnection("h4", "111");// 获取数据库连接
  11. query(conn); //方法名调用数据库连接
  12. releaseConnection(conn);//释放数据库连接
  13. }
  14. //查询数据,定义的query方法
  15. public static void query(Connection conn){
  16. String Sql="select * from employees";
  17. try{
  18. Statement stmt=conn.createStatement(); //也可以使用PreparedStatement来做
  19. ResultSet rs=stmt.executeQuery(Sql);//执行sql语句并返还结束
  20. rs.afterLast(); //先跳到最后一行
  21. while(rs.previous()){//遍历结果集 ,向上一行
  22. System.out.println("人员编号:"+rs.getString("employee_id")+"工资:"+rs.getString("salary"));
  23. }
  24. if(rs !=null){
  25. try{
  26. rs.close();
  27. } catch (SQLException e){
  28. e.printStackTrace();
  29. }
  30. }
  31. if(stmt !=null){
  32. try{
  33. stmt.close();
  34. }catch(SQLException e){
  35. e.printStackTrace();
  36. }
  37. }
  38. if(conn !=null){
  39. try{
  40. conn.close();
  41. }catch(SQLException e){
  42. e.printStackTrace();
  43. }
  44. }
  45. }catch(Exception e){
  46. e.printStackTrace();
  47. }
  48. }
  49. //数据库连接
  50. public static Connection getConnection(String user, String pass) {
  51. Connection conn = null;//声明连接对象
  52. String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  53. String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  54. + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  55. try {
  56. Class.forName(driver);// 注册(加载)驱动程序
  57. conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. return conn;
  62. }
  63. //释放数据库连接
  64. public static void releaseConnection(Connection conn) {
  65. try {
  66. if (conn != null)
  67. conn.close();
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }

2.插入数据

  1. 1 package javacore1;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.DriverManager;
  4. 4 import java.sql.SQLException;
  5. 5 import java.sql.Statement;
  6. 6 public class Insert {
  7. 7 public static void main(String[] args) {
  8. 8 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  9. 9 insert(conn); //方法名调用数据库连接
  10. 10 releaseConnection(conn);// 释放数据库连接
  11. 11 }
  12. 12 //插入数据
  13. 13 public static void insert(Connection conn) {
  14. 14 try {
  15. 15 String sql = "insert into employees(employee_id,last_name,salary,department_id,userid)"
  16. 16 + " values ('100010', 'xiaogou', '7000','004','9')"; // 插入数据的sql语句
  17. 17 Statement stmt1 =conn.createStatement(); // 创建用于执行静态sql语句的Statement对象
  18. 18 int count = stmt1.executeUpdate(sql); // 执行插入操作的sql语句,并返回插入数据的个数
  19. 19 System.out.println("向biao中插入了 " + count + " 条数据"); //输出插入操作的处理结果
  20. 20 conn.close(); //关闭数据库连接
  21. 21 } catch (SQLException e) {
  22. 22 e.printStackTrace();
  23. 23 }
  24. 24 }
  25. 25 //数据库连接
  26. 26 public static Connection getConnection(String user, String pass) {
  27. 27 Connection conn = null;//声明连接对象
  28. 28 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  29. 29 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  30. 30 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  31. 31 try {
  32. 32 Class.forName(driver);// 注册(加载)驱动程序
  33. 33 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  34. 34 } catch (Exception e) {
  35. 35 e.printStackTrace();
  36. 36 }
  37. 37 return conn;
  38. 38 }
  39. 39 //释放数据库连接
  40. 40 public static void releaseConnection(Connection conn) {
  41. 41 try {
  42. 42 if (conn != null)
  43. 43 conn.close();
  44. 44 } catch (Exception e) {
  45. 45 e.printStackTrace();
  46. 46 }
  47. 47 }
  48. 48 }

3.删除数据

  1. 1 package javacore1;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.DriverManager;
  4. 4 import java.sql.SQLException;
  5. 5 import java.sql.Statement;
  6. 6 public class Delete {
  7. 7 public static void main(String[] args) {
  8. 8 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  9. 9 delete(conn); //方法名调用数据库连接;
  10. 10 releaseConnection(conn);// 释放数据库连接
  11. 11 }
  12. 12 //删除数据
  13. 13 public static void delete(Connection conn){
  14. 14 String Sql = "delete from employees where employee_id=100009";
  15. 15 try {
  16. 16 Statement stmt = conn.createStatement();// 或者用PreparedStatement方法
  17. 17 stmt.executeUpdate(Sql);//执行sql语句
  18. 18 if (stmt != null) {
  19. 19 try {
  20. 20 stmt.close();
  21. 21 } catch (SQLException e) {
  22. 22 e.printStackTrace();
  23. 23 }
  24. 24 }
  25. 25 } catch (SQLException e) {
  26. 26 e.printStackTrace();
  27. 27 }
  28. 28
  29. 29 }
  30. 30 //数据库连接
  31. 31 public static Connection getConnection(String user, String pass) {
  32. 32 Connection conn = null;//声明连接对象
  33. 33 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  34. 34 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  35. 35 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  36. 36 try {
  37. 37 Class.forName(driver);// 注册(加载)驱动程序
  38. 38 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  39. 39 } catch (Exception e) {
  40. 40 e.printStackTrace();
  41. 41 }
  42. 42 return conn;
  43. 43 }
  44. 44 // 释放数据库连接
  45. 45 public static void releaseConnection(Connection conn) {
  46. 46 try {
  47. 47 if (conn != null)
  48. 48 conn.close();
  49. 49 } catch (Exception e) {
  50. 50 e.printStackTrace();
  51. 51 }
  52. 52 }
  53. 53 }

4.更新数据

  1. 1 package javacore1;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.DriverManager;
  4. 4 import java.sql.SQLException;
  5. 5 import java.sql.Statement;
  6. 6 public class Update {
  7. 7 public static void main(String[] args) {
  8. 8 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  9. 9 update(conn); //方法名调用数据库连接
  10. 10 releaseConnection(conn);// 释放数据库连接
  11. 11 }
  12. 12 //更新数据
  13. 13 public static void update(Connection conn){
  14. 14 String Sql = "update employees set salary=8000 where employee_id=100005";
  15. 15 try {
  16. 16 Statement stmt1 = conn.createStatement();//或者用PreparedStatement方法
  17. 17 stmt1.executeUpdate(Sql);//执行sql语句
  18. 18 if (stmt1 != null) {
  19. 19 try {
  20. 20 stmt1.close();
  21. 21 } catch (SQLException e) {
  22. 22 e.printStackTrace();
  23. 23 }
  24. 24 }
  25. 25 } catch (SQLException e) {
  26. 26 e.printStackTrace();
  27. 27 }
  28. 28 }
  29. 29 //数据库连接
  30. 30 public static Connection getConnection(String user, String pass) {
  31. 31 Connection conn = null;//声明连接对象
  32. 32 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  33. 33 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  34. 34 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  35. 35 try {
  36. 36 Class.forName(driver);// 注册(加载)驱动程序
  37. 37 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  38. 38 } catch (Exception e) {
  39. 39 e.printStackTrace();
  40. 40 }
  41. 41 return conn;
  42. 42 }
  43. 43 // 释放数据库连接
  44. 44 public static void releaseConnection(Connection conn) {
  45. 45 try {
  46. 46 if (conn != null)
  47. 47 conn.close();
  48. 48 } catch (Exception e) {
  49. 49 e.printStackTrace();
  50. 50 }
  51. 51 }
  52. 52 }

5.转账(事物处理)

JDBC处理事务通过关闭连接的自动提交实现的:

Connection.setAutoCommit(false);
提交事务:
    Connection.commit();
回滚事务
    回滚部分:
       Connection.rollback(Savepoint);
   全部回滚:
      Connection.rollback();

  1. 1 package javacore1;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.DriverManager;
  4. 4 import java.sql.Statement;
  5. 5 public class TransCash {
  6. 6 public static void main(String[] args) {
  7. 7 Connection conn = getConnection("h3", "111111");// 获取数据库连接
  8. 8 transCash(conn); //方法名调用数据库连接
  9. 9 releaseConnection(conn);// 释放数据库连接
  10. 10 }
  11. 11 //转账(数据调换)(原来数据为100和500,实现这个功能后变成500和100,其实说白了就是更新数据,改数据)
  12. 12 public static void transCash(Connection conn){
  13. 13 Statement stmt = null;
  14. 14 try{
  15. 15 conn.setAutoCommit(false);//关闭自动提交
  16. 16 String sql = "update employees set salary=500 where employee_id=100001";
  17. 17 stmt = conn.createStatement();
  18. 18 stmt.executeUpdate(sql);
  19. 19 sql = "update employees set salary=100 where employee_id=100002";
  20. 20 stmt.executeUpdate(sql);//执行sql语句,上面的两个工资将会调换
  21.  
  22. //int i=1/0;如果这样,那么将不可以完成任务,因为这是一个完整的事物,有一点失败,将全部失败
  23. 21 conn.commit();//提交事务
  24. 22 }catch(Exception e){
  25. 23 e.printStackTrace();
  26. 24 }finally{
  27. 25 try{
  28. 26 if(stmt != null){ stmt.close();
  29. 27 }catch(Exception e){
  30. 28 e.printStackTrace();
  31. 29 }
  32. 30 }
  33. 31 }
  34. 32 //数据库连接
  35. 33 public static Connection getConnection(String user, String pass) {
  36. 34 Connection conn = null;//声明连接对象
  37. 35 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  38. 36 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  39. 37 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  40. 38 try {
  41. 39 Class.forName(driver);// 注册(加载)驱动程序
  42. 40 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  43. 41 } catch (Exception e) {
  44. 42 e.printStackTrace();
  45. 43 }
  46. 44 return conn;
  47. 45 }
  48. 46 //释放数据库连接
  49. 47 public static void releaseConnection(Connection conn) {
  50. 48 try {
  51. 49 if (conn != null)
  52. 50 conn.close();
  53. 51 } catch (Exception e) {
  54. 52 e.printStackTrace();
  55. 53 }
  56. 54 }
  57. 55 }

6.查找存储过程无参数的

  1. 1 package javacore1;
  2. 2 import java.sql.CallableStatement;
  3. 3 import java.sql.Connection;
  4. 4 import java.sql.DriverManager;
  5. 5 import java.sql.SQLException;
  6. 6 public class ExecProc {
  7. 7 public static void main(String[] args) {
  8. 8 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  9. 9 execProc(conn);//方法名调用数据库连接
  10. 10 releaseConnection(conn);// 释放数据库连接
  11. 11 }
  12. 12 //调用无参存储过程;
  13. 13 public static void execProc(Connection conn){
  14. 14 String sql = "{call raisesalary}";
  15. 15 try {
  16. 16 CallableStatement cstmt = conn.prepareCall(sql);
  17. 17 cstmt.executeUpdate();
  18. 18 } catch (SQLException e) {
  19. 19 e.printStackTrace();
  20. 20 }
  21. 21 }
  22. 22 //数据库连接
  23. 23 public static Connection getConnection(String user, String pass) {
  24. 24 Connection conn = null;//声明连接对象
  25. 25 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  26. 26 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  27. 27 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  28. 28 try {
  29. 29 Class.forName(driver);// 注册(加载)驱动程序
  30. 30 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  31. 31 } catch (Exception e) {
  32. 32 e.printStackTrace();
  33. 33 }
  34. 34 return conn;
  35. 35 }
  36. 36 // 释放数据库连接
  37. 37 public static void releaseConnection(Connection conn) {
  38. 38 try {
  39. 39 if (conn != null)
  40. 40 conn.close();
  41. 41 } catch (Exception e) {
  42. 42 e.printStackTrace();
  43. 43 }
  44. 44 }
  45. 45 }

7.查找存储过程有参数的

  1. 1 package javacore1;
  2. 2 import java.sql.CallableStatement;
  3. 3 import java.sql.Connection;
  4. 4 import java.sql.DriverManager;
  5. 5 import java.sql.Types;
  6. 6 public class GetMulti {
  7. 7 public static void main(String[] args) {
  8. 8 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  9. 9 int cnt = getMulti(conn); //查找存储过程;
  10. 10 System.out.println("人员编号:" + cnt);
  11. 11 releaseConnection(conn);// 释放数据库连接
  12. 12 }
  13. 13 //查找存储过程有参
  14. 14 public static int getMulti(Connection conn) {
  15. 15 int dept_id = 100001;
  16. 16 int cnt = 0;
  17. 17 String sql = "{call calc_emp_count(?,?)}";
  18. 18 try {
  19. 19 CallableStatement cstmt = conn.prepareCall(sql);//初始化Statement对象
  20. 20 cstmt.setInt(1, dept_id);//CallableStatement.setxxx(参数,值)或者(数字,值),而PreparedStatement.setxxx(数字,值)只能这样
  21. 21 cstmt.setInt(2, cnt); //
  22. 22 cstmt.registerOutParameter(2, Types.INTEGER);//声明输出参数
  23. 23 cstmt.executeUpdate();//执行sql语句
  24. 24 cnt = cstmt.getInt(2);//获取结果
  25. 25 if (cstmt != null) {
  26. 26 cstmt.close();// 释放Statement对象
  27. 27 }
  28. 28 } catch (Exception e) {
  29. 29 e.printStackTrace();
  30. 30 }
  31. 31 return cnt;
  32. 32 }
  33. 33 //数据库连接
  34. 34 public static Connection getConnection(String user, String pass) {
  35. 35 Connection conn = null;//声明连接对象
  36. 36 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  37. 37 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  38. 38 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  39. 39 try {
  40. 40 Class.forName(driver);// 注册(加载)驱动程序
  41. 41 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  42. 42 } catch (Exception e) {
  43. 43 e.printStackTrace();
  44. 44 }
  45. 45 return conn;
  46. 46 }
  47. 47 //释放数据库连接
  48. 48 public static void releaseConnection(Connection conn) {
  49. 49 try {
  50. 50 if (conn != null)
  51. 51 conn.close();
  52. 52 } catch (Exception e) {
  53. 53 e.printStackTrace();
  54. 54 }
  55. 55 }
  56. 56 }

8.普通处理

  1. 1 package javacore1;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.Date;
  4. 4 import java.sql.DriverManager;
  5. 5 import java.sql.PreparedStatement;
  6. 6 import java.sql.SQLException;
  7. 7 public class PlanInsert {
  8. 8 public static void main(String[] args) {
  9. 9 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  10. 10 planInsert(conn); //方法名调用数据库连接
  11. 11 releaseConnection(conn);// 释放数据库连接
  12. 12 }
  13. 13 //普通处理
  14. 14 public static void planInsert(Connection conn) {
  15. 15 try {
  16. 16 String sql = "insert into test_user1(userid,username,loadingtime)"
  17. 17 + " values (?,?,?)"; // 插入数据的sql语句
  18. 18 PreparedStatement pstmt=conn.prepareStatement(sql);
  19. 19 long startTime=System.currentTimeMillis();
  20. 20 for(int i=0;i<1000;i++){
  21. 21 pstmt.setLong(1, i);
  22. 22 pstmt.setString(2, "user"+i);
  23. 23 pstmt.setDate(3, new Date(System.currentTimeMillis()));
  24. 24 pstmt.executeUpdate();
  25. 25 }
  26. 26 System.out.println("总共耗时:"+(System.currentTimeMillis() - startTime));
  27. 27 pstmt.close(); //关闭数据库连接
  28. 28 } catch (SQLException e) {
  29. 29 e.printStackTrace();
  30. 30 }
  31. 31 }
  32. 32 //数据库连接
  33. 33 public static Connection getConnection(String user, String pass) {
  34. 34 Connection conn = null;//声明连接对象
  35. 35 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  36. 36 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  37. 37 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  38. 38 try {
  39. 39 Class.forName(driver);// 注册(加载)驱动程序
  40. 40 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  41. 41 } catch (Exception e) {
  42. 42 e.printStackTrace();
  43. 43 }
  44. 44 return conn;
  45. 45 }
  46. 46 //释放数据库连接
  47. 47 public static void releaseConnection(Connection conn) {
  48. 48 try {
  49. 49 if (conn != null)
  50. 50 conn.close();
  51. 51 } catch (Exception e) {
  52. 52 e.printStackTrace();
  53. 53 }
  54. 54 }
  55. 55 }

9.批量处理

获得原来JDBC事务的模式:

boolean currentTransactionModle = con.getAutoCommit();

设置成事务模式(关闭自动提交):
con.setAutoCommit(false);
Statement stm = con.createStatement();
三个异构的sql语句:
String sql1 = "delete from user where id = 8";
String sql2 = "update user set name='java' where id = 7";
String sql3 = "insert into user(name,password) values('jdbc','jdbc')";
添加到Statement的批量处理缓冲区中:
stm.addBatch(sql1);
stm.addBatch(sql2);
stm.addBatch(sql3);
执行批量更新:
stm.executeBatch();
提交本次批量更新的事务:
con.commit();
回复原来的事务模式:
con.setAutoCommit(currentTransactionModle);

  1. 1 package javacore1;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.Date;
  4. 4 import java.sql.DriverManager;
  5. 5 import java.sql.PreparedStatement;
  6. 6 import java.sql.SQLException;
  7. 7 public class BatchInsert {
  8. 8 public static void main(String[] args) {
  9. 9 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  10. 10 batchInsert(conn); //方法名调用数据库连接
  11. 11 releaseConnection(conn);// 释放数据库连接
  12. 12 }
  13. 13 //批量插入的速度要比普通处理的速度快
  14. 14 public static void batchInsert(Connection conn) {
  15. 15 try {
  16. 16 String sql = "insert into test_user1(userid,username,loadingtime)"
  17. 17 + " values (?,?,?)"; // 插入数据的sql语句
  18. 18 PreparedStatement pstmt=conn.prepareStatement(sql);
  19. 19 long startTime=System.currentTimeMillis();
  20. 20 for(int i=0;i<1000;i++){
  21. 21 pstmt.setLong(1, i);
  22. 22 pstmt.setString(2, "user"+i);
  23. 23 pstmt.setDate(3, new Date(System.currentTimeMillis()));
  24. 24 pstmt.addBatch();//添加到批量处理
  25. 25 }
  26. 26 int[] result=pstmt.executeBatch();
  27. 27 System.out.println("总共耗时:"+(System.currentTimeMillis() - startTime));
  28. 28 pstmt.close(); //关闭数据库连接
  29. 29 } catch (SQLException e) {
  30. 30 e.printStackTrace();
  31. 31 }
  32. 32 }
  33. 33 //数据库连接
  34. 34 public static Connection getConnection(String user, String pass) {
  35. 35 Connection conn = null;//声明连接对象
  36. 36 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  37. 37 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  38. 38 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  39. 39 try {
  40. 40 Class.forName(driver);// 注册(加载)驱动程序
  41. 41 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  42. 42 } catch (Exception e) {
  43. 43 e.printStackTrace();
  44. 44 }
  45. 45 return conn;
  46. 46 }
  47. 47 //释放数据库连接
  48. 48 public static void releaseConnection(Connection conn) {
  49. 49 try {
  50. 50 if (conn != null)
  51. 51 conn.close();
  52. 52 } catch (Exception e) {
  53. 53 e.printStackTrace();
  54. 54 }
  55. 55 }
  56. 56 }

10.分页显示

  1. 1 package javacore1;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.DriverManager;
  4. 4 import java.sql.PreparedStatement;
  5. 5 import java.sql.ResultSet;
  6. 6 import java.sql.SQLException;
  7. 7 public class Paging {
  8. 8 public static void main(String[] args) {
  9. 9 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  10. 10 paging(conn,1,3);//方法名调用数据库连接,且定义显示第几行到第几行
  11. 11 releaseConnection(conn);// 释放数据库连接
  12. 12 }
  13. 13 //分页查询
  14. 14 public static void paging(Connection conn,int startIndex,int total){
  15. 15 try{
  16. 16 String sql="select * from employees limit ?,?";
  17. 17 PreparedStatement pstmt=conn.prepareStatement(sql);
  18. 18 pstmt.setInt(1, startIndex);
  19. 19 pstmt.setInt(2, total);
  20. 20 ResultSet rs=pstmt.executeQuery();
  21. 21 while(rs.next()){
  22. 22 System.out.print("工号:"+rs.getInt(1));
  23. 23 System.out.println("部门编号:"+rs.getInt("department_id"));
  24. 24 }rs.close();
  25. 25 pstmt.close();
  26. 26 }catch(SQLException e){
  27. 27 e.printStackTrace();
  28. 28 }
  29. 29 }
  30. 30 //数据库连接
  31. 31 public static Connection getConnection(String user, String pass) {
  32. 32 Connection conn = null;//声明连接对象
  33. 33 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  34. 34 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  35. 35 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  36. 36 try {
  37. 37 Class.forName(driver);// 注册(加载)驱动程序
  38. 38 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  39. 39 } catch (Exception e) {
  40. 40 e.printStackTrace();
  41. 41 }
  42. 42 return conn;
  43. 43 }
  44. 44 // 释放数据库连接
  45. 45 public static void releaseConnection(Connection conn) {
  46. 46 try {
  47. 47 if (conn != null)
  48. 48 conn.close();
  49. 49 } catch (Exception e) {
  50. 50 e.printStackTrace();
  51. 51 }
  52. 52 }
  53. 53 }

11.结果集可以滚动

  1. 1 package javacore1;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.DriverManager;
  4. 4 import java.sql.PreparedStatement;
  5. 5 import java.sql.ResultSet;
  6. 6 import java.sql.SQLException;
  7. 7 public class ScrpllResult {
  8. 8 public static void main(String[] args) {
  9. 9 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  10. 10 scrpllResult(conn);//方法名调用数据库连接
  11. 11 releaseConnection(conn);// 释放数据库连接
  12. 12 }
  13. 13 //结果集滚动显示
  14. 14 public static void scrpllResult(Connection conn){
  15. 15 try{
  16. 16 String sql="select * from employees"; //结果集可以滚动 //并发性,结果集只读,不可以修改
  17. 17 PreparedStatement pstmt=conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  18. 18 ResultSet rs=pstmt.executeQuery();
  19. 19 while(rs.next()){ //向下一行遍历
  20. 20 System.out.print("工号:"+rs.getLong(1));
  21. 21 System.out.println("名字"+rs.getString("last_name"));
  22. 22 }while(rs.previous()){//向上一行遍历
  23. 23 System.out.print("工号:"+rs.getLong(1));
  24. 24 System.out.println("工资"+rs.getInt("salary"));
  25. 25 }
  26. 26 rs.absolute(6);//表示直接跳到第几行
  27. 27 if(rs.next()){
  28. 28 System.out.print("工号:"+rs.getLong(1));
  29. 29 System.out.println("..........部门编号:"+rs.getString("department_id"));
  30. 30 }
  31. 31 rs.close();
  32. 32 pstmt.close();
  33. 33 }catch(SQLException e){
  34. 34 e.printStackTrace();
  35. 35 }
  36. 36 }
  37. 37 //数据库连接
  38. 38 public static Connection getConnection(String user, String pass) {
  39. 39 Connection conn = null;//声明连接对象
  40. 40 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  41. 41 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  42. 42 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  43. 43 try {
  44. 44 Class.forName(driver);// 注册(加载)驱动程序
  45. 45 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  46. 46 } catch (Exception e) {
  47. 47 e.printStackTrace();
  48. 48 }
  49. 49 return conn;
  50. 50 }
  51. 51 // 释放数据库连接
  52. 52 public static void releaseConnection(Connection conn) {
  53. 53 try {
  54. 54 if (conn != null)
  55. 55 conn.close();
  56. 56 } catch (Exception e) {
  57. 57 e.printStackTrace();
  58. 58 }
  59. 59 }
  60. 60 }

五.把上面十一个放在一起当然最方便省事。(需要用谁,只需把前面的注释打开便行)

  1. 1 package javacore1;
  2. 2 import java.sql.CallableStatement;
  3. 3 import java.sql.Connection;
  4. 4 import java.sql.Date;
  5. 5 import java.sql.DriverManager;
  6. 6 import java.sql.PreparedStatement;
  7. 7 import java.sql.ResultSet;
  8. 8 import java.sql.SQLException;
  9. 9 import java.sql.Statement;
  10. 10 import java.sql.Types;
  11. 11 public class jdbcMySql {
  12. 12 public static void main(String[] args) {
  13. 13 Connection conn = getConnection("h3", "111111"); // 获取数据库连接
  14. 14 //query(conn); //1.查询数据
  15. 15 //insert(conn);//2.插入数据
  16. 16 //delete(conn);//3.删除数据
  17. 17 //update(conn);//4.更新数据
  18. 18 //transCash(conn);//5.转账
  19. 19 //execProc(conn);//6.查找存储过程无参数的
  20. 20 //int cnt = getMulti(conn);//7.查找存储过程有参数的
  21. 21 //System.out.println("人员编号:" + cnt);//查找存储过程有参数的
  22. 22 //planInsert(conn);//8.普通处理
  23. 23 //batchInsert(conn);//9.批量处理
  24. 24 // paging(conn,1,3);//10.分页显示
  25. 25 //scrpllResult(conn);//11.结果集可以滚动
  26. 26 releaseConnection(conn);// 释放数据库连接
  27. 27 }
  28. 28 //1.查询数据,定义的query方法
  29. 29 public static void query(Connection conn){
  30. 30 String Sql="select * from employees";
  31. 31 try{
  32. 32 Statement stmt=conn.createStatement(); //也可以使用PreparedStatement来做
  33. 33 ResultSet rs=stmt.executeQuery(Sql);//执行sql语句并返还结束
  34. 34
  35. 35 while(rs.next()){//遍历结果集
  36. 36 System.out.println("人员编号:"+rs.getString("employee_id")+"工资:"+rs.getString("salary"));
  37. 37 }
  38. 38 if(rs !=null){
  39. 39 try{
  40. 40 rs.close();
  41. 41 } catch (SQLException e){
  42. 42 e.printStackTrace();
  43. 43 }
  44. 44 }
  45. 45 if(stmt !=null){
  46. 46 try{
  47. 47 stmt.close();
  48. 48 }catch(SQLException e){
  49. 49 e.printStackTrace();
  50. 50 }
  51. 51 }
  52. 52 if(conn !=null){
  53. 53 try{
  54. 54 conn.close();
  55. 55 }catch(SQLException e){
  56. 56 e.printStackTrace();
  57. 57 }
  58. 58 }
  59. 59 }catch(Exception e){
  60. 60 e.printStackTrace();
  61. 61 }
  62. 62 }
  63. 63 //2.插入数据
  64. 64 public static void insert(Connection conn) {
  65. 65 try {
  66. 66 String sql = "insert into employees(employee_id,last_name,salary,department_id,userid)"
  67. 67 + " values ('100010', 'xiaogou', '7000','004','9')"; // 插入数据的sql语句
  68. 68 Statement stmt1 =conn.createStatement(); // 创建用于执行静态sql语句的Statement对象
  69. 69 int count = stmt1.executeUpdate(sql); // 执行插入操作的sql语句,并返回插入数据的个数
  70. 70 System.out.println("向biao中插入了 " + count + " 条数据"); //输出插入操作的处理结果
  71. 71 conn.close(); //关闭数据库连接
  72. 72 } catch (SQLException e) {
  73. 73 e.printStackTrace();
  74. 74 }
  75. 75 }
  76. 76 //3.删除数据
  77. 77 public static void delete(Connection conn){
  78. 78 String Sql = "delete from employees where employee_id=100009";
  79. 79 try {
  80. 80 Statement stmt = conn.createStatement();// 或者用PreparedStatement方法
  81. 81 stmt.executeUpdate(Sql);//执行sql语句
  82. 82 if (stmt != null) {
  83. 83 try {
  84. 84 stmt.close();
  85. 85 } catch (SQLException e) {
  86. 86 e.printStackTrace();
  87. 87 }
  88. 88 }
  89. 89 } catch (SQLException e) {
  90. 90 e.printStackTrace();
  91. 91 }
  92. 92
  93. 93 }
  94. 94 //4.更新数据
  95. 95 public static void update(Connection conn){
  96. 96 String Sql = "update employees set salary=8000 where employee_id=100005";
  97. 97 try {
  98. 98 Statement stmt1 = conn.createStatement();//或者用PreparedStatement方法
  99. 99 stmt1.executeUpdate(Sql);//执行sql语句
  100. 100 if (stmt1 != null) {
  101. 101 try {
  102. 102 stmt1.close();
  103. 103 } catch (SQLException e) {
  104. 104 e.printStackTrace();
  105. 105 }
  106. 106 }
  107. 107 } catch (SQLException e) {
  108. 108 e.printStackTrace();
  109. 109 }
  110. 110 }
  111. 111 //5.转账(数据调换)(原来数据为100和500,实现这个功能后变成500和100,其实说白了就是更新数据,改数据)
  112. 112 public static void transCash(Connection conn){
  113. 113 Statement stmt = null;
  114. 114 try{
  115. 115 conn.setAutoCommit(false);//关闭自动提交
  116. 116 String sql = "update employees set salary=500 where employee_id=100001";
  117. 117 stmt = conn.createStatement();
  118. 118 stmt.executeUpdate(sql);
  119. 119 sql = "update employees set salary=100 where employee_id=100002";
  120. 120 stmt.executeUpdate(sql);//执行sql语句,上面的两个工资将会调换
  121. 121 conn.commit();//提交事务
  122. 122 }catch(Exception e){
  123. 123 e.printStackTrace();
  124. 124 }finally{
  125. 125 try{
  126. 126 if(stmt != null)stmt.close();
  127. 127 }catch(Exception e){
  128. 128 e.printStackTrace();
  129. 129 }
  130. 130 }
  131. 131 }
  132. 132 //6.调用无参存储过程;
  133. 133 public static void execProc(Connection conn){
  134. 134 String sql = "{call raisesalary}";
  135. 135 try {
  136. 136 CallableStatement cstmt = conn.prepareCall(sql);
  137. 137 cstmt.executeUpdate();
  138. 138 } catch (SQLException e) {
  139. 139 e.printStackTrace();
  140. 140 }
  141. 141 }
  142. 142 //7.查找存储过程有参
  143. 143 public static int getMulti(Connection conn) {
  144. 144 int dept_id = 100001;
  145. 145 int cnt = 0;
  146. 146 String sql = "{call calc_emp_count(?,?)}";
  147. 147 try {
  148. 148 CallableStatement cstmt = conn.prepareCall(sql);//初始化Statement对象
  149. 149 cstmt.setInt(1, dept_id);//CallableStatement.setxxx(参数,值)或者(数字,值),而PreparedStatement.setxxx(数字,值)只能这样
  150. 150 cstmt.setInt(2, cnt); //
  151. 151 cstmt.registerOutParameter(2, Types.INTEGER);//声明输出参数
  152. 152 cstmt.executeUpdate();//执行sql语句
  153. 153 cnt = cstmt.getInt(2);//获取结果
  154. 154 if (cstmt != null) {
  155. 155 cstmt.close();// 释放Statement对象
  156. 156 }
  157. 157 } catch (Exception e) {
  158. 158 e.printStackTrace();
  159. 159 }
  160. 160 return cnt;
  161. 161 }
  162. 162 //8.普通处理
  163. 163 public static void planInsert(Connection conn) {
  164. 164 try {
  165. 165 String sql = "insert into test_user1(userid,username,loadingtime)"
  166. 166 + " values (?,?,?)"; // 插入数据的sql语句
  167. 167 PreparedStatement pstmt=conn.prepareStatement(sql);
  168. 168 long startTime=System.currentTimeMillis();
  169. 169 for(int i=0;i<1000;i++){
  170. 170 pstmt.setLong(1, i);
  171. 171 pstmt.setString(2, "user"+i);
  172. 172 pstmt.setDate(3, new Date(System.currentTimeMillis()));
  173. 173 pstmt.executeUpdate();
  174. 174 }
  175. 175 System.out.println("总共耗时:"+(System.currentTimeMillis() - startTime));
  176. 176 pstmt.close(); //关闭数据库连接
  177. 177 } catch (SQLException e) {
  178. 178 e.printStackTrace();
  179. 179 }
  180. 180 }
  181. 181 //9.批量插入的速度要比普通处理的速度快
  182. 182 public static void batchInsert(Connection conn) {
  183. 183 try {
  184. 184 String sql = "insert into test_user1(userid,username,loadingtime)"
  185. 185 + " values (?,?,?)"; // 插入数据的sql语句
  186. 186 PreparedStatement pstmt=conn.prepareStatement(sql);
  187. 187 long startTime=System.currentTimeMillis();
  188. 188 for(int i=0;i<1000;i++){
  189. 189 pstmt.setLong(1, i);
  190. 190 pstmt.setString(2, "user"+i);
  191. 191 pstmt.setDate(3, new Date(System.currentTimeMillis()));
  192. 192 pstmt.addBatch();//添加到批量处理
  193. 193 }
  194. 194 int[] result=pstmt.executeBatch();
  195. 195 System.out.println("总共耗时:"+(System.currentTimeMillis() - startTime));
  196. 196 pstmt.close(); //关闭数据库连接
  197. 197 } catch (SQLException e) {
  198. 198 e.printStackTrace();
  199. 199 }
  200. 200 }
  201. 201 //10.分页查询
  202. 202 public static void paging(Connection conn,int startIndex,int total){
  203. 203 try{
  204. 204 String sql="select * from employees limit ?,?";
  205. 205 PreparedStatement pstmt=conn.prepareStatement(sql);
  206. 206 pstmt.setInt(1, startIndex);
  207. 207 pstmt.setInt(2, total);
  208. 208 ResultSet rs=pstmt.executeQuery();
  209. 209 while(rs.next()){
  210. 210 System.out.print("工号:"+rs.getInt(1));
  211. 211 System.out.println("部门编号:"+rs.getInt("department_id"));
  212. 212 }rs.close();
  213. 213 pstmt.close();
  214. 214 }catch(SQLException e){
  215. 215 e.printStackTrace();
  216. 216 }
  217. 217 }
  218. 218 //11.结果集滚动显示
  219. 219 public static void scrpllResult(Connection conn){
  220. 220 try{
  221. 221 String sql="select * from employees"; //结果集可以滚动 //并发性,结果集只读,不可以修改
  222. 222 PreparedStatement pstmt=conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  223. 223 ResultSet rs=pstmt.executeQuery();
  224. 224 while(rs.next()){ //向下一行遍历
  225. 225 System.out.print("工号:"+rs.getLong(1));
  226. 226 System.out.println("名字"+rs.getString("last_name"));
  227. 227 }while(rs.previous()){//向上一行遍历
  228. 228 System.out.print("工号:"+rs.getLong(1));
  229. 229 System.out.println("工资"+rs.getInt("salary"));
  230. 230 }
  231. 231 rs.absolute(6);//表示直接跳到第几行
  232. 232 if(rs.next()){
  233. 233 System.out.print("工号:"+rs.getLong(1));
  234. 234 System.out.println("..........部门编号:"+rs.getString("department_id"));
  235. 235 }
  236. 236 rs.close();
  237. 237 pstmt.close();
  238. 238 }catch(SQLException e){
  239. 239 e.printStackTrace();
  240. 240 }
  241. 241 }
  242. 242 //数据库连接
  243. 243 public static Connection getConnection(String user, String pass) {
  244. 244 Connection conn = null;//声明连接对象
  245. 245 String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
  246. 246 String url = "jdbc:mysql://localhost:3306/test?" // 数据库URL
  247. 247 + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
  248. 248 try {
  249. 249 Class.forName(driver);// 注册(加载)驱动程序
  250. 250 conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
  251. 251 } catch (Exception e) {
  252. 252 e.printStackTrace();
  253. 253 }
  254. 254 return conn;
  255. 255 }
  256. 256 //释放数据库连接
  257. 257 public static void releaseConnection(Connection conn) {
  258. 258 try {
  259. 259 if (conn != null)
  260. 260 conn.close();
  261. 261 } catch (Exception e) {
  262. 262 e.printStackTrace();
  263. 263 }
  264. 264 }
  265. 265 }

 六.安装下载的数据库驱动程序jar包,不同的数据库需要不同的驱动程序(但是安装方法都是一样的)

在使用JDBC编程时需要连接数据库,导入JAR包是必须的,导入其它的jar包方法同样如此,导入的方法是

打开eclipse
1.右击要导入jar包的项目,点properties

2.左边选择java build path,右边选择libraries

3.选择add External jars

4.选择jar包的按照路径下的
确定后就行了。

Java连接MySQL的最新驱动包下载地址

http://www.mysql.com/downloads/connector/j

1.鼠标放在你建的根目录上面。右击,然后选择最下面的properties。

2.然后左边选择java build path,右边选择libraries ,在选择右边的add External jars ,选择jar包的路径,点击确定就可以了

3.装好后,图如下出现你要添加的包。

以上大部分内容整理自网络,感谢猿猿们的无私奉献~~具体的步骤、强大的互联网上都比较容易查询的到,这里不再赘述

JDBC连接(MySql)数据库步骤,以及查询、插入、删除、更新等十一个处理数据库信息的功能的更多相关文章

  1. 在Eclipse中通过JDBC连接MySQL步骤,非常详细!

    通过JDBC连接MySQL基本步骤代码讲解步骤可能遇到的Bug基本步骤JDBC访问MySQL 1.加载JDBC驱动器—>哪个project需要,就添加到该project的jdbc文件夹下,我的j ...

  2. jdbc 01: 连接mysql,并实现数据插入

    jdbc连接mysql,并实现数据插入 package com.examples.jdbc.o1_连接与插入; import java.sql.*; /* jdbc数据库连接六步 */ public ...

  3. 分页查询信息(使用jdbc连接mysql数据库实现分页查询任务)

             分页查询信息       使用jdbc连接mysql数据库实现分页查询任务 通过mysql数据库提供的分页机制,实现商品信息的分页查询功能,将查询到的信息显示到jsp页面上. 本项目 ...

  4. JDBC连接MySQL数据库及演示样例

    JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...

  5. JDBC连接MySQL数据库及示例

      JDBC是Sun公司制定的一个可以用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一 ...

  6. java用JDBC连接MySQL数据库的详细知识点

    想实现java用JDBC连接MySQL数据库.需要有几个准备工作: 1.下载Connector/J的库文件,下载Connector/J的官网地址:http://www.mysql.com/downlo ...

  7. JDBC连接MySQL数据库代码模板

    下面这个例子是最简单的JDBC连接MySQL数据库的例子. 一般步骤: 1.注册驱动: 2.建立连接: 3.创建语句: 4.处理结果: 5.释放资源. 注意: 1.软件开发环境:MyEclipse 8 ...

  8. JDBC——使用JDBC连接MySQL数据库

    在JDBC--什么是JDBC一文中我们已经介绍了JDBC的基本原理. 这篇文章我们聊聊如何使用JDBC连接MySQL数据库. 一.基本操作 首先我们需要一个数据库和一张表: CREATE DATABA ...

  9. java jdbc 连接mysql数据库 实现增删改查

    好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...

随机推荐

  1. SecureCRT - 使用方法和技巧

    1. 保活防掉线选项 -> 会话选项 -> 终端勾选 自动重新连接, 发送协议 NO-OP 每60秒 2. 拷贝与粘贴的设置选项 -> Global options -> Te ...

  2. spring源码

    今天看了看spring对于视图解析的源码,发现还不是那些思想,internalResourceView里的一个渲染方法 protected void exposeModelAsRequestAttri ...

  3. erlang nif小结

    一.nif获取string参数的两种方式 1.eif_get_string 实例如下: static ERL_NIF_TERM erl_sm4_encrypt(ErlNifEnv* env, int ...

  4. app.use(express.static)设置静态文件目录小解

    app.use(path, function, [, function]) 功能: 为path注册中间函数,即根据path使用不同函数进行处理,默认path是"/",也就是处理任何 ...

  5. POJ 3737/三分

    题目链接[http://poj.org/problem?id=3737] 题意:给出一个圆锥的表面积,求最大的体积,并输出最大体积的时候的圆锥的高度和底面积. 方法一: 根据定理:圆锥的表面积一定的时 ...

  6. HDU 5348 MZL's endless loop

    乱搞题...第一直觉是混合图的欧拉通路,但是感觉并没有多大关系.最终AC的做法是不断的寻找欧拉通路,然后给边标号.所有边访问了一遍,所有点访问了一遍,效率是o(n+m).不存在-1的情况. #incl ...

  7. Chapter 2 Open Book——1

    The next day was better… and worse. 明天会更好也会更坏. It was better because it wasn't raining yet, though t ...

  8. CDN技术详解及实现原理

    CDN技术详解 一本好的入门书是带你进入陌生领域的明灯,<CDN技术详解>绝对是带你进入CDN行业的那盏最亮的明灯.因此,虽然只是纯粹的重点抄录,我也要把<CDN技术详解>的精 ...

  9. git分支--branch

    分支创建: $ git branch testing 显示分支: $ git branch iss53 * master testing 如果需要查看每一个分支的最后一次提交 $ git branch ...

  10. 安卓OpenGL入门

    1.先用一个GLSurfaceView作为画布,然后自定义一个Renderer继承自Renderer,把这个自定义的Renderer通过setRenderer()设置给GLSurfaceView就可以 ...