package Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//MySQL数据库连接类
public class JDBC_MySQL {
//driver地址
private static final String DBDRIVER = "com.mysql.jdbc.Driver";
//数据库地址 书写形式 协议:子协议:数据源标识
private static final String DBURL = "jdbc:mysql://localhost:3306/";
//用户名
private static final String USER = "root";
//密码
private static final String PASS = "root";
Connection conn = null;
Statement st = null; //执行静态SQL语句
PreparedStatement pst = null; //执行动态SQL语句
ResultSet rs = null;
public void JDBCUtils(){
try {
//加载驱动,在连接数据库之前,首先要加载想要连接的数据库到JVM中,通过java.lang.Class这个静态类实现
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//提供数据库连接URL
//创建数据库连接
String DBName = "userregion";
try {
conn = (Connection) DriverManager.getConnection(DBURL+DBName, USER, PASS);
} catch (SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
//创建Statement(预处理SQL)
try {
st = conn.createStatement();
} catch (SQLException e1) {
e1.printStackTrace();
}
String sql = "select * from testtbl";
//执行SQL
try {
rs = st.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
//处理结果
try {
while(rs.next()){
String name = rs.getString("id");
System.out.println(name);
}
} catch (SQLException e) {
e.printStackTrace();
}
//关闭JDBC对象(关闭流)
try {
if(rs != null){
rs.close();
}
if(st != null){
st.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//MySQL数据库连接类
public class JDBC_MySQL {
//driver地址
private static final String DBDRIVER = "com.mysql.jdbc.Driver";
//数据库地址 书写形式 协议:子协议:数据源标识
private static final String DBURL = "jdbc:mysql://localhost:3306/";
//用户名
private static final String USER = "root";
//密码
private static final String PASS = "root";
Connection conn = null;
Statement st = null; //执行静态SQL语句
PreparedStatement pst = null; //执行动态SQL语句
ResultSet rs = null;
public void JDBCUtils(){
try {
//加载驱动,在连接数据库之前,首先要加载想要连接的数据库到JVM中,通过java.lang.Class这个静态类实现
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//提供数据库连接URL
//创建数据库连接
String DBName = "userregion";
try {
conn = (Connection) DriverManager.getConnection(DBURL+DBName, USER, PASS);
} catch (SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
//创建Statement(预处理SQL)
try {
st = conn.createStatement();
} catch (SQLException e1) {
e1.printStackTrace();
}
String sql = "select * from testtbl";
//执行SQL
try {
rs = st.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
//处理结果
try {
while(rs.next()){
String name = rs.getString("id");
System.out.println(name);
}
} catch (SQLException e) {
e.printStackTrace();
}
//关闭JDBC对象(关闭流)
try {
if(rs != null){
rs.close();
}
if(st != null){
st.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}