導航:首頁 > 編程大全 > java連接資料庫代理

java連接資料庫代理

發布時間:2025-07-04 22:19:15

java怎樣連接mysql資料庫

1、java連接MySQL資料庫需要有一個驅動jar包

例如:mysql-connector-java-5.1.26-bin.jar,

package.test.jsp;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;

importjavax.naming.spi.DirStateFactory.Result;

publicclassDbConnection{
privatestaticConnectionconn;
publicDbConnection(){
Stringdrivername="com.mysql.jdbc.Driver";
Stringusername="root";
Stringurl="jdbc:mysql://localhost/jsptest?useUnicode=true&characterEncoding=UTF-8";
Stringpassword="";
//載入驅動
try{
Class.forName(drivername);
}catch(ClassNotFoundExceptione){
System.out.println("驅動載入失敗!");
e.printStackTrace();
}
//建立連接
try{
conn=DriverManager.getConnection(url,username,password);
}catch(SQLExceptione){
System.out.println("資料庫連接失敗!");
e.printStackTrace();
}


}
//getResultSet
publicResultSetGetResultSet(Stringsql)
{
ResultSetrs=null;
//statemanage
try{
Statementst=conn.createStatement();
rs=st.executeQuery(sql);
}catch(SQLExceptione){
System.out.println("狀態管理器創建失敗");
e.printStackTrace();
}
returnrs;

}
//DML
publicintDML(Stringsql)
{
intcount=-1;
try{
Statementstatement=conn.createStatement();
count=statement.executeUpdate(sql);
}catch(SQLExceptione){
System.out.println("狀態管理器創建失敗");
e.printStackTrace();
}
returncount;
}
}

3、可以新建service類來調用連接類裡面的getResultSet方法和DML,實現自己所需用的功能。

閱讀全文

與java連接資料庫代理相關的資料

熱點內容
百寶app是做什麼的 瀏覽:325
vb程序比較大小程序 瀏覽:951
clinuxsleep頭文件 瀏覽:342
蘋果6swifi模塊 瀏覽:744
3d的備份文件在哪裡 瀏覽:824
硬體編程的硬體哪個好 瀏覽:534
數控編程按車床分類有哪些 瀏覽:275
qq文件下載到哪裡 瀏覽:434
pdf文件交叉排序 瀏覽:350
word文字轉化成pdf怎樣讓文件變小 瀏覽:587
三星手機微信相機許可權 瀏覽:146
編程的無效關鍵字有哪些 瀏覽:322
網路公司軟文 瀏覽:303
怎麼查看別人qq相冊密碼是什麼軟體 瀏覽:602
pdf怎麼放文件 瀏覽:946
蘋果無滑鼠怎麼復制文件 瀏覽:820
三國殺java安卓 瀏覽:299
百度體育app 瀏覽:113
如何比對兩個excel表格數據是否一致 瀏覽:243
釘釘我的工作文件在哪裡 瀏覽:815

友情鏈接