JDBC JAVA資料庫插入語句

來源:互聯網
上載者:User

標籤:pre   row   import   mobile   ror   com   this   樣本   參數   

https://zhidao.baidu.com/question/540539955.html?fr=iks&word=jdbc+sql%D3%EF%BE%E4&ie=gbk

JDBC JAVA資料庫插入語句報錯
你妹的,寫了預留位置你又不用東西填,sql當然錯了,報的sql不完整
兩種方案
A。  valuses後的四個問號直接寫上參數
B。用import java.sql.PreparedStatement;包下的PreparedStatement方法
預編的方式進行填充樣本
PreparedStatement ps = null;

sql = "insert into record(cId,bId,mId,borrowDate,returnDate) values(?,?,?,?,?)";
ps.setInt(1,第一個值);
ps.setInt(2,第二個值);
ps.setInt(3,第三個值);
ps.setInt(4,第四個值);
//提交1234必須寫setInt是舉例子你看自己的那些欄位是什麼類型的
ps.executeUpdate();

 

範例:

String sql=""+
  "insert into shopping"+
  "(user_name,sex,age,birthday,email,mobile," +                             (注意“+”號的用法,之前我漏掉了一個加號,導致報錯Syntax error on token ""values("", delete this token,語法錯誤)
  "create_user,create_date,update_user,update_date,isdel)"+
  "values("+
  "?,?,?,?,?,?,?,current_date(),?,current_fste(),?)";

或者

String sql="insert into shopping(user_name,sex,age,birthday,email,mobile,create_user,create_date,update_user,update_date,isdel)values(?,?,?,?,?,?,?,current_date(),?,current_fste(),?)";

下面sql語句用參數填充:

PreparedStatement ptmt=conn.prepareStatement(sql);  //編譯sql語句
ptmt.setString(1, g.getUser_name());        //傳遞參數進來
ptmt.setInt(2,g.getSex());
ptmt.setInt(3, g.getAge());
ptmt.setDate(4, (Date) g.getBirthday());
ptmt.setString(5,g.getEmail());
ptmt.setString(6, g.getMobile());
ptmt.setString(7, g.getCreate_user());
ptmt.setString(8, g.getUpdate_user());
ptmt.setInt(9,g.getIsdel());
ptmt.execute();

JDBC JAVA資料庫插入語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.