准备工作:mysql-connector-java-5.1.6-bin.jar配置
package com.job;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class yy {
// JDBC 驱动器名称 和数据库地址
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//数据库的名称为 EXAMPLE
static final String DB_URL = "jdbc:mysql://localhost/EXAMPLE";
// 数据库用户和密码
static final String USER = "root";
static final String PASS = "123456";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//注册JDBC 驱动程序
Class.forName("com.mysql.jdbc.Driver");
//打开连接
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//执行查询
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, name, age FROM Students";
ResultSet rs = stmt.executeQuery(sql);
//得到和处理结果集
while(rs.next()){
//检索
int id = rs.getInt("id");
int age = rs.getInt("age");
String name = rs.getString("name");
//显示
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", Name: " + name);
System.out.println();
}
//清理环境
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// JDBC 操作错误
se.printStackTrace();
}catch(Exception e){
// Class.forName 错误
e.printStackTrace();
}finally{
//这里一般用来关闭资源的
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
****************************************************prepareStatement+UPDATE用法:有利于高效地执行多次使用的 SQL 语句
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//执行查询
System.out.println("Creating statement...");
String sql = "UPDATE Students set age=? WHERE id=?";
stmt = conn.prepareStatement(sql);
//将值绑定到参数,参数从左至右序号为1,2...
stmt.setInt(1, 6667); // 绑定 age 的值(序号为1)
stmt.setInt(2, 3);
int rows1 = stmt.executeUpdate();
stmt.setInt(1, 6667); // 绑定 age 的值(序号为1)
stmt.setInt(2, 2); // 绑定 ID 的值
int rows = stmt.executeUpdate();
System.out.println("被影响的行数 : " + rows+rows1 );
***********************************************************************结果集rs导航方法
rs.next();意思是光标移动到最后一个数据出,可显示最后一个数据
rs.first();
rs.last();。。。
*************************************************************************************conn.setAutoCommit(false);关闭了自动提交后,我们要提交更改,可以调用 commit() 方法:
不要忘记,在catch块内添加回滚事务,表示操作出现异常,撤销事务:conn.rollback();
public class JdbcTest {
// JDBC 驱动器名称 和数据库地址
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//数据库的名称为 EXAMPLE
static final String DB_URL = "jdbc:mysql://localhost/EXAMPLE";
// 数据库用户和密码
static final String USER = "root";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//注册JDBC 驱动程序
Class.forName("com.mysql.jdbc.Driver");
//打开连接
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
conn.setAutoCommit(false);
//执行查询
System.out.println("Creating statement...");
stmt = conn.createStatement();
//插入
String sql = "INSERT INTO Students " +
"VALUES (5, 20, 'Rose')";
stmt.executeUpdate(sql);
//查找
sql = "SELECT id, name, age FROM Students";
ResultSet rs = stmt.executeQuery(sql);
//提交事务
conn.commit();
//得到和处理结果集
while(rs.next()){
//检索
int id = rs.getInt("id");
int age = rs.getInt("age");
String name = rs.getString("name");
//显示
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", Name: " + name);
System.out.println();
}
//清理环境
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// JDBC 操作错误
se.printStackTrace();
// conn.rollback();
try{
if(conn!=null)
conn.rollback();
}catch(SQLException se2){
se2.printStackTrace();
}
}catch(Exception e){
// Class.forName 错误
e.printStackTrace();
}finally{
//这里一般用来关闭资源的
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
*************************************************************************************8批量处理sql语句
Statement stmt = conn.createStatement();
// 关闭自动提交
conn.setAutoCommit(false);
// 创建 SQL 语句
String SQL = "INSERT INTO Students (id, name, age) VALUES(6,'Mike', 21)";
// 将 SQL 语句添加到批处理中
stmt.addBatch(SQL);
// 创建更多的 SQL 语句
String SQL = "INSERT INTO Students (id, name, age) VALUES(7, 'Angle', 23)";
// 将 SQL 语句添加到 批处理中
stmt.addBatch(SQL);
// 创建整数数组记录更新情况
int[] count = stmt.executeBatch();
//提交更改
conn.commit();
——————————
String SQL = "INSERT INTO Employees (id, name, age) VALUES(?, ?, ?)";
// 创建 PrepareStatement 对象
PreparedStatemen pstmt = conn.prepareStatement(SQL);
//关闭自动连接
conn.setAutoCommit(false);
// 绑定参数
pstmt.setInt( 1, 8 );
pstmt.setString( 2, "Cindy" );
pstmt.setInt( 3, 17 );
// 添入批处理
pstmt.addBatch();
// 绑定参数
pstmt.setInt( 1, 9 );
pstmt.setString( 2, "Jeff" );
pstmt.setInt( 3, 22 );
// 添入批处理
pstmt.addBatch();
//创建数组记录更改
int[] count = pstmt.executeBatch();
//提交更改
conn.commit();
callablestatement 调用存储过程和函数 connection.preparecall(过程、函数名字)
过程无返回值用out代替 存储函数有返回值return
例子:xxx=connection.preparecall("{call 过程名(?,?,?)}")
xxx.setInt(1,xx)
xxx.setInt(2,xx)
xxx.execute()
xxx.registeroutparameter(3,Types.INTEGER)//设置返回值类型
int result=xxx.get(3)