[AppleScript] 纯文本查看 复制代码
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
private static String driver; //数据库驱动,在配置文件中读取。
private static String url; //指向你自己的数据库。
private static String user; //连接数据库的账号
private static String password; //连接数据库的密码
private static Statement statement; //用于接收sql语句执行平台
private static Connection conn; //用于接收数据库连接对象
private static ResultSet rs; //用于接收结果集
static {
//创建Properties对象
Properties prop = new Properties();
try {
//读取配置文件,获取配置信息。
prop.load(new BufferedReader(new FileReader("src/mysql.properties")));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
//注册驱动
Class.forName(driver);
} catch (FileNotFoundException e) {
System.out.println("找不到配置文件.");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("读取数据库配置文件错误");
e.printStackTrace();
}
}
/**
* 获取数据库连接
* @return Connection连接对象
*/
public static Connection getConnection() {
try {
conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 执行sql语句并返回结果集
* @param sql 要执行的sql语句
* @return ResultSet结果集
*/
public static ResultSet executeQuery(String sql) {
try {
statement = getConnection().createStatement();
rs = statement.executeQuery(sql);
return rs;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 执行增、删、改sql语句
* @param sql 要执行的sql语句
* @return 数据库中受影响的行数
*/
public static int executeUpdate(String sql) {
try {
statement = getConnection().createStatement();
int count = statement.executeUpdate(sql);
return count;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
/**
* 获取预编译对象
* @param sql 预编译对象要编译的sql语句
* @return PreparedStatement预编译对象
*/
public static PreparedStatement getPreparedStatement(String sql) {
try {
return getConnection().prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 关闭连接
*/
public static void close() {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(statement!=null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
statement = null;
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
[AppleScript] 纯文本查看 复制代码
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.itcast.utils.JDBCUtils;
public class JDBCUtilsTest {
public static void main(String[] args) throws SQLException {
//JDBCUtils.executeQuery(String sql)测试
String sql1 = "select * from table01;";
ResultSet rs1 = JDBCUtils.executeQuery(sql1);
while(rs1.next()) {
System.out.println(rs1.getObject(1));
}
JDBCUtils.close();
//JDBCUtils.getPreparedStatement(String sql)测试
String sql2 = "select * from table01 where id=?;";
PreparedStatement ps = JDBCUtils.getPreparedStatement(sql2);
ps.setInt(1, 1);
ResultSet rs2 = ps.executeQuery();
while(rs2.next()) {
System.out.println(rs2.getObject("name"));
}
JDBCUtils.close();
}
}