2个回答
展开全部
我封装的DB类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class mySQLDB {
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myBBS", "root" , "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement createStmt(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet executeQuery(Statement stmt ,String sql){
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static PreparedStatement prepareStmt(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void close(Statement stmt) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class mySQLDB {
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myBBS", "root" , "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement createStmt(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet executeQuery(Statement stmt ,String sql){
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static PreparedStatement prepareStmt(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void close(Statement stmt) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}
展开全部
Class.forName("com.mysql.jdbc.Driver");
connsql = "jdbc:mysql://"+【yourhost】"+【yourdatabase】+"?user="+【yourusername】+"&password="+【yourpass】+"&" +
"useUnicode=true&characterEncoding=UTF-8";
Connection conn = DriverManager.getConnection(connsql);
Statement stat = conn.createStatement();
//存
String insert_sql = "insert into yourtable(key1,key2,key4) value(value1,value2,value4)";
stat.executeUpdate(insert_sql);
//取
String select_sql = "select * from yourtable where id = 1";
ResultSet rs = stat.executeQuery(select_sql);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
}
//删
String del_sql = "delete form yourtable where id = 1";
stat.executeUpdate(del_sql);
//改
String update_sql = "update yourtable set name = "+"\'"+newname+"\'"+" where id = 1";
stat.executeUpdate(update_sql);
if(rs != null)rs.close();
if(stat != null)stat.close();
if(conn != null)conn.close();
connsql = "jdbc:mysql://"+【yourhost】"+【yourdatabase】+"?user="+【yourusername】+"&password="+【yourpass】+"&" +
"useUnicode=true&characterEncoding=UTF-8";
Connection conn = DriverManager.getConnection(connsql);
Statement stat = conn.createStatement();
//存
String insert_sql = "insert into yourtable(key1,key2,key4) value(value1,value2,value4)";
stat.executeUpdate(insert_sql);
//取
String select_sql = "select * from yourtable where id = 1";
ResultSet rs = stat.executeQuery(select_sql);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
}
//删
String del_sql = "delete form yourtable where id = 1";
stat.executeUpdate(del_sql);
//改
String update_sql = "update yourtable set name = "+"\'"+newname+"\'"+" where id = 1";
stat.executeUpdate(update_sql);
if(rs != null)rs.close();
if(stat != null)stat.close();
if(conn != null)conn.close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询