導航:首頁 > 編程語言 > java寫入mysql

java寫入mysql

發布時間:2022-08-29 11:29:55

1. java程序中如何將密碼寫入mysql庫中

// 1. 注冊驅動
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// Mysql 的驅動

// 2. 獲取資料庫的連接
java.sql.Connection conn = java.sql.DriverManager.getConnection(
"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK", "root", null);

// 3. 獲取表達式
java.sql.Statement stmt = conn.createStatement();

// 執行插入數據的 SQL
stmt.executeUpdate("insert into MyUser(username, password) values('JDBC 中文測試', '密碼')");

// 4. 執行 SQL
java.sql.ResultSet rs = stmt.executeQuery("select * from MyUser");

// 5. 顯示結果集裡面的數據
while(rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
System.out.println();
}

// 6. 釋放資源
rs.close();
stmt.close();
conn.close();

2. 用java向mysql中寫入數據,報錯,感覺我寫的沒問題,求大神幫忙!!

sql語句有錯誤,把你這條sql拿到資料庫上去執行一下,看是否表名,欄位名等錯誤

3. Java使用mySql寫入數據是出現的問題,字元集我已經調為一樣的,都是UTF_8,但還是有問題,代碼如下:

這個是你sql語句錯誤了。我教你,你把這條語句現在資料庫運行對了再粘貼到jsp頁面中運行。同時考慮編碼問題,如果你實在不會,留下你QQ號,我幫你解決。

4. java怎樣存數據到mysql資料庫

用java連接資料庫啊。下面給點例子可以參考,不大好,湊合看

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;

publicclassJdbcConnection
{
privateStringdriver;
privateStringdbname;
privateStringusername;
privateStringpassword;
privateStringurl;
privateConnectioncon;
privatePreparedStatementpstmt;
privateResultSetrs;

publicJdbcConnection(){
this.driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
this.dbname=資料庫名";
this.username="資料庫賬號";
this.password="資料庫密碼";
this.url="地址加資料庫名";
}

//創建連接方法
publicConnectiongetconnection()
{
try
{
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);

}catch(ClassNotFoundExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returncon;
}

//關閉連接方法
publicvoidcoloseConnection(Connectioncon,ResultSetrs,PreparedStatementpstmt)
{
if(rs!=null)
{
try
{
rs.close();
}catch(SQLExceptione)
{
e.printStackTrace();
}
}
if(pstmt!=null)
{
try
{
pstmt.close();
}catch(SQLExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
if(con!=null)
{
try
{
con.close();
}catch(SQLExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}

publicConnectiongetCon()
{
returncon;
}


publicvoidsetCon(Connectioncon)
{
this.con=con;
}


()
{
returnpstmt;
}


publicvoidsetPstmt(PreparedStatementpstmt)
{
this.pstmt=pstmt;
}


publicResultSetgetRs()
{
returnrs;
}


publicvoidsetRs(ResultSetrs)
{
this.rs=rs;
}


publicStringgetDriver()
{
returndriver;
}
publicvoidsetDriver(Stringdriver)
{
this.driver=driver;
}
publicStringgetDbname()
{
returndbname;
}
publicvoidsetDbname(Stringdbname)
{
this.dbname=dbname;
}
publicStringgetUsername()
{
returnusername;
}
publicvoidsetUsername(Stringusername)
{
this.username=username;
}
publicStringgetPassword()
{
returnpassword;
}
publicvoidsetPassword(Stringpassword)
{
this.password=password;
}
publicStringgetUrl()
{
returnurl;
}
publicvoidsetUrl(Stringurl)
{
this.url=url;
}

}

5. java怎麼添加數據到mysql資料庫

導入資料庫數據 和 鏈接到資料庫 概念不一樣 不知道樓主是要干什麼

不管怎麼樣 都要下載JDBC驅動 不同的資料庫有不同的地址 就不帖了

簡單的 鏈接mysql 資料庫 用API獲取Connection 就算連接上了
至於怎麼用API 網路一搜一堆

導入數據 eclipse 有資料庫資源管理器 在DataBaseConnection 新建 填好參數 就可以獲取數據了 沒圖我說個j8

6. java寫入mysql資料庫亂碼

應該是中文亂碼問題

可以加上編碼格式的設置,同時保證與資料庫編碼格式一致。

7. 如何用java將集合的元素到入mysql

你是說將java中集合的所有元素插入到資料庫中嗎?
List<String> list = new ArrayList<String>();
list.add("元素1");
list.add("元素2");
list.add("元素3");
list.add("元素4");
for(int i=0;i<list.size();i++){
String item = list.get(i);
String sql = "insert into table test(test)values(item)";
int flag = save(sql);//如果flag == 1則表示數據插入資料庫成功,否則表示插入失敗
}
//注釋:你只需要寫一個插入數據到資料庫的方法save(String sql)即可。

8. JAVA中如何將數據存入mysql

首先通過JDBC連接沒mysqll伺服器,接著就寫SQL往mysqll裡面插入數據啊

閱讀全文

與java寫入mysql相關的資料

熱點內容
老夫子pdf 瀏覽:302
基於matlab的圖像壓縮編碼 瀏覽:77
麥克納姆輪編程 瀏覽:156
昂科威15壓縮比 瀏覽:100
安卓車機wifi安全性如何選擇 瀏覽:388
華三伺服器如何外接硬碟 瀏覽:585
csgo創意工坊如何尋找專用伺服器 瀏覽:314
程序員抓蟲子動圖 瀏覽:600
廣數挑四頭螺紋怎麼編程 瀏覽:644
embraco壓縮機是哪國的 瀏覽:907
為什麼伺服器流量那麼貴 瀏覽:258
讀取pdf文件內容 瀏覽:490
如何給腳本鏈接加密 瀏覽:82
多伺服器雲部署方案 瀏覽:443
如何把一個安卓應用改為中文版 瀏覽:450
帶求補器的源碼 瀏覽:731
程序員掉入能力陷阱 瀏覽:672
中百超市多點app如何充值 瀏覽:331
仙劍奇俠傳4codex未加密 瀏覽:194
如何搭建伺服器ss埠 瀏覽:62