使用JNDI數(shù)據(jù)源啊。

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、西疇ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的西疇網(wǎng)站制作公司
大約是 java/jsp/servlet/ ---- JNDI ---JDBC -- Datbase.
package com.db.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import otc.OtcDBcontrol;
public class DBUtil {
public Connection conn;
public ResultSet rs;
public Statement st;
public PreparedStatement ps;
public DataSource ds;
public boolean flag = false;
public DBUtil() throws SQLException {
getConnection();
}
/**
* 打開連接
* @throws SQLException
*/
public void getConnection() throws SQLException {
try {
if (conn == null) {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/otc?user=rootpassword=123useUnicode=truecharacterEncoding=UTF-8");
conn.setAutoCommit(false);
}
} catch (Exception e) {
e.printStackTrace();
}
st = conn.createStatement();
}
/**
* 關(guān)閉連接
* @throws SQLException
*/
public void close() throws SQLException {
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
}
/**
* 查詢
* @param sql
* @return
* @throws SQLException
*/
public ResultSet query(String sql) throws SQLException {
System.out.println(sql);
rs = st.executeQuery(sql);
return rs;
}
/**
* 增刪改
* @param sql
* @return
* @throws SQLException
*/
public boolean update(String sql) throws SQLException {
System.out.println(sql);
int s = 1;
s = st.executeUpdate(sql);
if (s != 0) {
conn.setAutoCommit(false);
conn.commit();
flag = true;
} else {
conn.rollback();
flag = false;
}
return flag;
}
}
這個是代碼,只需要改你自己的數(shù)據(jù)庫用戶名密碼就可以了~
private static final String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost:1433;databaseName=company";
private static final String UserName="sa";
private static final String UserPass="sqlpass";
public static Connection getCon(){
Connection con = null;
try {
Class.forName(Driver);
con=DriverManager.getConnection(URL,UserName,UserPass); //連接數(shù)據(jù)庫
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
你把url改成mysql的就行了
還有把mysql的jar復(fù)制到lib文件下。
分享名稱:j2ee怎么連mysql mysql80鏈接eclipse
網(wǎng)址分享:http://www.chinadenli.net/article48/dddjoep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、響應(yīng)式網(wǎng)站、用戶體驗、網(wǎng)站設(shè)計、域名注冊、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)