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

熱點內容
哪個領域演算法好 瀏覽:380
用命令行編譯java 瀏覽:677
筆趣閣app哪個是正版手機app 瀏覽:427
程序員這個工作好嗎 瀏覽:898
agps定位伺服器地址 瀏覽:659
用水做的解壓玩具怎麼做 瀏覽:418
安卓411能下載什麼 瀏覽:304
小海龜logo命令 瀏覽:493
java製作界面 瀏覽:895
台達plc編程電纜製作 瀏覽:249
30多歲當程序員 瀏覽:442
怎樣把表格轉換成pdf 瀏覽:514
行列式分解演算法 瀏覽:291
mscoreedll是什麼文件夾修復 瀏覽:697
什麼app測臉型 瀏覽:43
cf手游刀戰演算法 瀏覽:318
北京壓縮米飯 瀏覽:554
施工員與程序員哪個好 瀏覽:207
vc與word編程 瀏覽:951
androidmenu文字顏色 瀏覽:983