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

熱點內容
買安卓手機怎麼在官網買 瀏覽:116
詩詞入門PDF 瀏覽:355
毒app是什麼單位 瀏覽:58
如何自己編譯android系統 瀏覽:786
phpmysqlpdomysqli 瀏覽:802
php修改sql語句 瀏覽:714
android有道api 瀏覽:381
撓耳營水表用的什麼app 瀏覽:49
戰錘40kpdf 瀏覽:377
java判斷字元串是漢字 瀏覽:464
2017初級教材pdf 瀏覽:453
松下空調壓縮機品牌 瀏覽:819
python復選框 瀏覽:748
反詐中心app是什麼時候出來的 瀏覽:588
魔獸世界需要什麼伺服器地址 瀏覽:191
啥是單片機休眠 瀏覽:106
什麼音樂app最好 瀏覽:929
牙膏自製解壓神器 瀏覽:329
卸載linuxwps 瀏覽:228
threadjavarunnable 瀏覽:360