mysql数据库的连接

Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.getConnection("jdbc:oracle:thin:localhost:datebase","user","password");

JDBC连接数据库的4个步骤:
 *1、/**********加载驱动程序*************/
*   Class.forName();
*2、/**********/连接数据库*************/
*   conn=DriverManager.getConnection(url,username,password);
 *3、/*****数据库的增删查改操作******/
*   pstmt=conn.prepareStatement(sql); //实例化对象
*   result = pstmt.executeQuery() ;//执行 SQL 查询并返回ResultSet 对象
 *4、/**********数据库资源的关闭*************/
 *   result.close() ;
*   pstmt.close() ;
*     conn.close();
 */


/*****************JDBC连接Oracle数据库************************/
         // 驱动程序就是之前在classpath中配置的jdbc的驱动程序的jar包中
         public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
         // 连接地址是由各个数据库生产商单独提供的,所以需要单独记住
         public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:MLDN";
         // 连接数据库的用户名
         public static final String DBUSER = "scott";
         // 连接数据库的密码
         public static final String DBPASS = "tiger";
public static void main(String[] args) throws Exception {
                   Connection conn = null; // 表示数据库的连接的对象
                   PreparedStatement pstmt = null ;  // 表示数据库的更新操作
                   ResultSet result = null ;// 表示接收数据库的查询结果
                   String keyWord = "" ;
                   String sql = "SELECT pid,name,age,birthday,salary FROM person WHERE name LIKE ? OR birthday LIKE ?" ;
                   // 1、使用Class类加载驱动程序
                   Class.forName(DBDRIVER);
                   // 2、连接数据库
                   conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
                   // 3、PreparedStatement接口需要通过Connection接口进行实例化操作
                   pstmt = conn.prepareStatement(sql) ;
                   //设置数据库的模糊查询
                   pstmt.setString(1,"%"+keyWord+"%") ;
                   pstmt.setString(2,"%"+keyWord+"%") ;
                   // 执行SQL语句,查询数据库
                   result = pstmt.executeQuery() ;
                   while(result.next()){// 是否有下一行数据
                            int pid = result.getInt(1) ;
                            String name = result.getString(2) ;
                            int age = result.getInt(3) ;
                            Date birthday = result.getDate(4) ;
                            float salary = result.getFloat(5) ;
                            System.out.print("pid = " + pid + ";") ;
                            System.out.print("name = " + name + ";") ;
                            System.out.print("age = " + age + ";") ;
                            System.out.print("birthday = " + birthday + ";") ;
                            System.out.println("salary = " + salary + ";") ;
                   }
                   // 4、关闭数据库
                   result.close() ;
                   pstmt.close() ;
                   conn.close();
         }
}
 

 
二、JDBC批处理:
       批处理:多条SQL语句可以一次性执行完毕,成为批处理操作。
在Statement接口上定义了一个addBatch()方法,此方法可以加入批处理,之后使用executeBatch()方法执行批处理的操作。
package org.lxh.prepareddemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class BatchInsertDemo {
         // 驱动程序就是之前在classpath中配置的jdbc的驱动程序的jar包中
         public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
         // 连接地址是由各个数据库生产商单独提供的,所以需要单独记住
         public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:MLDN";
         // 连接数据库的用户名
         public static final String DBUSER = "scott";
         // 连接数据库的密码
         public static final String DBPASS = "tiger";
         public static void main(String[] args) throws Exception {
                   Connection conn = null; // 表示数据库的连接的对象
                   PreparedStatement pstmt = null; // 表示数据库的更新操作
                   String sql = "INSERT INTO person(pid,name,age,birthday,salary) VALUES (perseq.nextval,?,?,?,?) ";
                   System.out.println(sql) ;
                   // 1、使用Class类加载驱动程序
                   Class.forName(DBDRIVER);
                   // 2、连接数据库
                   conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
                   // 3、PreparedStatement接口需要通过Connection接口进行实例化操作
                   pstmt = conn.prepareStatement(sql) ;// 使用预处理的方式创建对象
                   for(int i=0;i<10;i++){
                            pstmt.setString(1, "lxh-" + i);// 第一个?号的内容
                            pstmt.setInt(2, 20 + i); // 第二个?号的内容
                            pstmt.setDate(3, new java.sql.Date(new java.util.Date().getTime()));
                            pstmt.setFloat(4, 900*i);
                            pstmt.addBatch() ;  // 增加批处理
                   }
                   // 执行SQL语句,更新数据库
                   int i[] = pstmt.executeBatch() ; //将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组
                   System.out.println(i.length);
                   // 4、关闭数据库
                   pstmt.close() ;
                   conn.close();
         }
}

 
、JDBC事务处理
         数据库事务处理,通过commit提交事务,通过rollback回滚事物。但在Connection操作所以的数据库更新属于立即更新的,如果进行事务处理的操作,则必须首先停止自动更新操作,然后所以的更新通过commit()方法进行提交,如果出现问题,则回滚。
package org.lxh.trandemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class TransactionDemo02 {
         // 驱动程序就是之前在classpath中配置的jdbc的驱动程序的jar包中
         public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
         // 连接地址是由各个数据库生产商单独提供的,所以需要单独记住
         public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:MLDN";
         // 连接数据库的用户名
         public static final String DBUSER = "scott";
         // 连接数据库的密码
         public static final String DBPASS = "tiger";
         public static void main(String[] args) throws Exception {
                   Connection conn = null; // 表示数据库的连接的对象
                   Statement stmt = null; // 表示数据库的更新操作
                   // 1、使用Class类加载驱动程序
                   Class.forName(DBDRIVER);
                   // 2、连接数据库
                   conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
                   conn.setAutoCommit(false) ;  // 取消自动提交实现事务要么全提交、要么全不提交,因为JDBC默认是自动提交的,取消了才能实现事务处理
                   // 3、Statement接口需要通过Connection接口进行实例化操作
                   stmt = conn.createStatement() ;
                   try{
                            stmt.addBatch("INSERT INTO person(pid,name,age,birthday,salary) VALUES (perseq.nextval,'张三',30,TO_DATE('1995-02-14','yyyy-mm-dd'),9000.0) ") ;
                            stmt.addBatch("INSERT INTO person(pid,name,age,birthday,salary) VALUES (perseq.nextval,'李四',30,TO_DATE('1995-02-14','yyyy-mm-dd'),9000.0) ") ;
                            stmt.addBatch("INSERT INTO person(pid,name,age,birthday,salary) VALUES (perseq.nextval,'王'五',30,TO_DATE('1995-02-14','yyyy-mm-dd'),9000.0) ") ;
                            stmt.addBatch("INSERT INTO person(pid,name,age,birthday,salary) VALUES (perseq.nextval,'赵六',30,TO_DATE('1995-02-14','yyyy-mm-dd'),9000.0) ") ;
                            stmt.addBatch("INSERT INTO person(pid,name,age,birthday,salary) VALUES (perseq.nextval,'孙七',30,TO_DATE('1995-02-14','yyyy-mm-dd'),9000.0) ") ;
                            conn.commit() ;// 全都没出差提交
                   }catch(Exception e){
                            conn.rollback() ;//出现了一个错误事物回滚
                   }
                   // 4、关闭数据库
                   stmt.close() ;
                   conn.close();
         }
}

四、JDBC:连接MySQL数据库
package org.lxh.mysqldemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JDBCMySQL {
         // 驱动程序就是之前在classpath中配置的jdbc的驱动程序的jar包中
         public static final String DBDRIVER = "com.mysql.jdbc.Driver";
         // 连接地址是由各个数据库生产商单独提供的,所以需要单独记住
         public static final String DBURL = "jdbc:mysql://localhost:3306/testSQL";
         // 连接数据库的用户名
         public static final String DBUSER = "root";
         // 连接数据库的密码
         public static final String DBPASS = "123456";
         public static void main(String[] args) throws Exception {
                   Connection conn = null; // 表示数据库的连接的对象
                   PreparedStatement pstmt = null; // 表示数据库的更新操作
                   String name = "张三";
                   int age = 30;
                   Date date = new SimpleDateFormat("yyyy-MM-dd").parse("1983-02-15");
                   float salary = 7000.0f;
                   String sql = "INSERT INTO person(name,age,birthday,salary) VALUES (?,?,?,?) ";
                   System.out.println(sql) ;
                   // 1、使用Class类加载驱动程序
                   Class.forName(DBDRIVER);
                   // 2、连接数据库
                   conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
                   // 3、PreparedStatement接口需要通过Connection接口进行实例化操作
                   pstmt = conn.prepareStatement(sql) ;// 使用预处理的方式创建对象
                   pstmt.setString(1, name) ;// 第一个?号的内容
                   pstmt.setInt(2, age) ;      // 第二个?号的内容
                   pstmt.setDate(3, new java.sql.Date(date.getTime())) ;
                   pstmt.setFloat(4,salary) ;
                   // 执行SQL语句,更新数据库
                   pstmt.executeUpdate();
                   // 4、关闭数据库
                   pstmt.close() ;
                   conn.close();
         }
}

猜你喜欢

转载自blog.csdn.net/sinat_26397681/article/details/52811157