導航:首頁 > 編程語言 > java訪問mysql

java訪問mysql

發布時間:2025-03-09 00:40:51

A. 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訪問mysql相關的資料

熱點內容
怎麼查看u盤加密區 瀏覽:181
台電加密是什麼格式 瀏覽:155
php論壇版塊在哪個文件夾 瀏覽:442
暗黑的伺服器為什麼維護 瀏覽:623
android內存溢出的原因 瀏覽:17
標志307的壓縮比是多少 瀏覽:636
伺服器啟動為什麼叫三聲 瀏覽:997
追風箏的人英文pdf 瀏覽:939
解壓小熊手機殼 瀏覽:346
成都市區建成面積演算法 瀏覽:660
智能家居單片機 瀏覽:97
買男裝用什麼app好 瀏覽:855
文件夾合並了怎麼拆開 瀏覽:260
波段副圖源碼無未來函數 瀏覽:89
livecn伺服器地址 瀏覽:259
程序員這個工作真的很吃香嗎 瀏覽:847
程序員和數學分析師待遇 瀏覽:681
壓縮氣彈簧怎麼拆 瀏覽:325
華為公有雲伺服器添加虛擬ip 瀏覽:211
程序員和運營哪個累 瀏覽:27