|
|
|
|
package javabean;
|
|
|
|
|
/********************
|
|
|
|
|
*@IDE eclipse
|
|
|
|
|
*@jdk 1.8.0_161
|
|
|
|
|
*********************/
|
|
|
|
|
import java.sql.*;
|
|
|
|
|
|
|
|
|
|
public class db_conn {
|
|
|
|
|
public Connection conn = null;
|
|
|
|
|
public ResultSet res = null;
|
|
|
|
|
public Statement st = null;
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
public db_conn() {
|
|
|
|
|
|
|
|
|
|
String URL="jdbc:mysql://localhost:3307/fly_ticket?useSSL=false&useUnicode=true&characterEncoding=UTF8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; //<2F><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>3306
|
|
|
|
|
String USER="root"; //<2F><><EFBFBD>ݿ<EFBFBD><DDBF>û<EFBFBD><C3BB><EFBFBD>
|
|
|
|
|
String PWD="123456"; //<2F><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
try{
|
|
|
|
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
|
|
|
|
}catch(ClassNotFoundException e){
|
|
|
|
|
System.out.println("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
|
|
|
|
|
System.out.println(e);
|
|
|
|
|
}
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
conn = DriverManager.getConnection(URL,USER,PWD);
|
|
|
|
|
// System.out.println("success");
|
|
|
|
|
st=conn.createStatement();
|
|
|
|
|
}catch(SQLException e){
|
|
|
|
|
System.out.println("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>ʧ<EFBFBD><CAA7>");
|
|
|
|
|
System.out.println(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
public int executeInsert(String sql){
|
|
|
|
|
int num=0;
|
|
|
|
|
try{
|
|
|
|
|
num=st.executeUpdate(sql);
|
|
|
|
|
}
|
|
|
|
|
catch(SQLException e){
|
|
|
|
|
System.out.println("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD><EFBFBD><EFBFBD>Ϣ:"+e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF>в<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
|
|
|
|
|
public ResultSet executeQuery(String sql){
|
|
|
|
|
res=null;
|
|
|
|
|
try{
|
|
|
|
|
res=st.executeQuery(sql);
|
|
|
|
|
}
|
|
|
|
|
catch(SQLException e){
|
|
|
|
|
System.out.print("<22><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ:"+e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
public int Update(String sql){
|
|
|
|
|
int num=0;
|
|
|
|
|
try{
|
|
|
|
|
num=st.executeUpdate(sql);
|
|
|
|
|
}catch(SQLException ex){
|
|
|
|
|
System.out.print("ִ<><D6B4><EFBFBD><EFBFBD><DEB8>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>"+ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
public int executeDelete(String sql){
|
|
|
|
|
int num=0;
|
|
|
|
|
try{
|
|
|
|
|
num=st.executeUpdate(sql);
|
|
|
|
|
}
|
|
|
|
|
catch(SQLException e){
|
|
|
|
|
System.out.print("ִ<><D6B4>ɾ<EFBFBD><C9BE><EFBFBD>д<EFBFBD><D0B4><EFBFBD>:"+e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
public void closeDB(){
|
|
|
|
|
try{
|
|
|
|
|
st.close();
|
|
|
|
|
conn.close();
|
|
|
|
|
}
|
|
|
|
|
catch(Exception e){
|
|
|
|
|
System.out.print("ִ<>йر<D0B9>Connection<6F><6E><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>:"+e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|