導航:首頁 > 源碼編譯 > javasql通訊錄源碼

javasql通訊錄源碼

發布時間:2023-05-23 17:40:32

java程序連接sql server2008資料庫:求給一個具體的代碼,和過程。

連接資料庫
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class TestDB {

public static void main(String[] args) {
try {
// 1.注冊驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("注冊驅動成功");

String url = ""jdbc:microsoft.sqlserver://127.0.0.1:1433;DatabaseName="; //資料庫IP,埠,資料庫名

String username = "";
String password = "";
// 載入驅動並獲取資料庫連接對象
Connection connection = DriverManager.getConnection(url, username,
password);
System.out.println("連接資料庫成功!");
// 創建Statement對象
Statement stmt = connection.createStatement();
// SQL語句
String sql = "insert into student(id,name,age,email,address) values(1,'tom',23,'[email protected]','England') ";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("插入成功!");
}
// 釋放資源
stmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}

catch (ClassNotFoundException e) {
e.printStackTrace();
}

}

}

編譯的資料庫處理
public class ConnectionManager{
public static final String DRIVER_CLASS ="";
public static final String URL = "";
public static final String USERNAME = "";
public static final String PASSWORD = "";

//獲取連接對象
public static Connection getConnection(){
Connection conn = null;
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
try{
if(rs!=null){
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}
if(conn!=null){
conn.close();
conn=null;
}
}
}
}
public class DBOperator{
public int executeUpdate(String sql,Object[] params){
int rows = 0;
Connection connn = ConnectionManager.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1,params[i]);//下標從1開始
}
}
rows = pstmt.executeUpdate();
ConnectionManager.closeAll(null,pstmt,conn);
return rows;
}
public ResultSet executeQuery(String sql,Object[] params){
Connetion conn = ConnectionManager.getConnection();
if(params!=null){
for(int i= 0;i<params.length;i++){
pstmt.setObject(i+1,params(i));
}
}
rs=pstmt.executeQuery();
ConnectionManager.closeAll(rs,pstmt,conn);
return rs;
}
}

/**
*statement連接資料庫
*/

public class TestOperatorGrade{
private static void updateGrade(int id,String name,String description){
Class.forName();//注冊驅動
Connection conn = DirverManager.getConnection("","","");
Statement stmt = conn.createStament();
String sql = "UPDATE grades set name = '"+name+"',description='"+description+"'WHere id="+id;
int rows = stmt.executeUpdate(sql);

}
}

這是java 程序的代碼

㈡ 幫分析一下 java.sql.SQLException: 源代碼 和 報錯 情況如下.

先檢查是不是密碼用戶名埠都正確
還不行就,別用localhost,改用127.0.0.1
還不行你就安裝sqlserver 2000 sp3
還不行,你看看是不是sqlserver禁止使用你的tcp/ip協議
還不行換個埠別用1433,1433好像有bug
再不行,你就祈禱吧

閱讀全文

與javasql通訊錄源碼相關的資料

熱點內容
截圖R命令 瀏覽:654
基於單片機的智能台燈設計 瀏覽:685
多餘app是怎麼兌換皮膚的 瀏覽:552
sql資料庫查詢表命令 瀏覽:551
簡單音樂網站源碼 瀏覽:644
運動健康app華為手錶怎麼連接 瀏覽:748
肌肉塑造全書pdf下載 瀏覽:796
安卓簡約拼圖用什麼軟體好 瀏覽:289
fx1n加密程序 瀏覽:844
淘客阿里雲伺服器 瀏覽:476
100壓縮打造 瀏覽:422
安卓手機怎麼和蘋果平板傳文件 瀏覽:973
開始選項卡中的頁眉和頁腳命令選項 瀏覽:424
pdf的字體怎麼改 瀏覽:856
python讀寫視頻 瀏覽:88
科魯茲壓縮機軸承 瀏覽:353
word文檔轉換成pdf文件找不到 瀏覽:27
組件注冊命令 瀏覽:760
安卓大屏導航用的是什麼運放 瀏覽:443
myandroidtools的備份 瀏覽:900