導航:首頁 > 編程語言 > java寫sql語句

java寫sql語句

發布時間:2023-06-02 07:48:27

java如何執行sql語句

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;


public class xxxx {

public static void main(String[] args) {

Connection con = null ;

Statement stmt = null ;

try {

Class.forName("com.mysql.jdbc.Driver"); //mysql為例 不一樣的資料庫所需的驅動包不一樣 連接語句略有不同

con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/資料庫名", "root", "密碼");

stmt = con.createStatement();

String sql = "insert into info values ('用戶', 'mima', 'piapiapia~')";

stmt.executeUpdate(sql);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if(stmt != null) {

stmt.close();

stmt = null;

}

if (con != null) {

con.close();

con = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}


}


② java模糊查詢中的sql語句怎麼寫

這語句是查詢任意位置出現的字元串了

String sql="select * from employeeform where ename like '%'"+fname+"'%'";

什麼資料庫呢。。。

③ java 中如何使用sql插入語句

public int save(Notices notice) throws SQLException{
String sql = "insert into noticeinfo(notice_title,notice_type,notice_content,notice_add_time,user_id,user_table_id,class_table_id,notice_state,is_important)"+"values(?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = super.getConnection().prepareStatement(sql);
pstmt.setString(1, notice.getNotice_title());
pstmt.setInt(2, notice.getNotice_type());
pstmt.setString(3, notice.getNotice_content());
pstmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
pstmt.setString(5, notice.getUser_id());
pstmt.setInt(6, notice.getUser_table_id());
pstmt.setInt(7, notice.getClass_table_id());
pstmt.setInt(8, notice.getNotice_state());
pstmt.setInt(9, notice.getIs_important());
int i = pstmt.executeUpdate();
return i;
}

④ 在java中寫一段很長的SQL語句該怎麼寫 我這個該怎麼改

說個簡單點的辦法,你這個SQL語句別分行,比如你的sql語句是這樣的。
select "+ uid +"from " +t_user_log +" where name = "+ name+ 「and date between '」+
date +"'"

寫成類似於這種的。
你這SQL語句我看著沒什麼問題配模,你扔到你資料庫里看看能不能查出來。我拼SQL語句一直都是先寫好SQL語句培神緩,然後把條件換成「」.雙引號和括弧再做處理,你這么走幾遍就能看出是瞎塌哪有問題了。

⑤ java中執行sql插入語句怎麼弄

1、Connection conn = DriverManager.getConnection(URL,資料庫登錄名,資料庫登錄密碼);//獲得資料庫連接。


2、Statement statement = con.createStatement(); //訪問資料庫。


3、ResultSet resultSet = statement.executeQuery(sql);//執行SQL語句。

閱讀全文

與java寫sql語句相關的資料

熱點內容
珠海存儲伺服器地址怎麼找 瀏覽:413
md5演算法字元串長度 瀏覽:4
可以二次虛化的雲伺服器 瀏覽:779
思科2500編程器固件 瀏覽:235
php開發桌面應用程序 瀏覽:904
支付寶app哪裡可以加油 瀏覽:71
路由器ttl刷編程器固件 瀏覽:719
縱向加密密鑰協商狀態時間 瀏覽:851
mc花雨庭伺服器有些什麼 瀏覽:809
linux製作網頁 瀏覽:19
xlsx加密忘記了怎麼辦 瀏覽:999
app湖北農信怎麼解約 瀏覽:426
在線編程教育項目 瀏覽:759
電信采購5萬台伺服器干什麼用 瀏覽:200
騰訊雲伺服器登錄地址 瀏覽:988
程序員在地鐵上寫字 瀏覽:555
解壓包未知文件格式怎麼辦 瀏覽:578
程序員破壞資料庫 瀏覽:331
sh格式如何編譯 瀏覽:344
虛擬伺服器雲主機哪個好 瀏覽:98