導航:首頁 > 編程語言 > 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相關的資料

熱點內容
卸載聯想app哪個好 瀏覽:716
php文字轉圖片 瀏覽:326
豆客後台怎麼加密碼 瀏覽:572
jpg轉換pdf破解版 瀏覽:976
php基礎書籍推薦 瀏覽:773
伺服器與外網不通如何驗證 瀏覽:349
電子版是不是就是文件夾 瀏覽:48
游戲屬性文件加密 瀏覽:460
如何讓安卓手機桌面圖標下移 瀏覽:526
ubuntuphp5環境搭建 瀏覽:99
賭癮解壓視頻 瀏覽:917
晉城移動dns伺服器地址 瀏覽:294
php開源文庫系統 瀏覽:134
android記事本源碼 瀏覽:405
安卓11小游戲怎麼玩法 瀏覽:188
gif有損壓縮 瀏覽:935
windows下安裝linux命令操作 瀏覽:842
米家app怎麼設置進門亮燈 瀏覽:652
任我行伺服器為什麼會影響截圖 瀏覽:296
安卓留言板怎麼刪除 瀏覽:18