java 中如何使用sql插入语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 中如何使用sql插入语句相关的知识,希望对你有一定的参考价值。
sql语句如下:insert into room
values('3-001','豪华间','双人间','0','null','null',' ','null')如何将其转换为Java语句中插入
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;
参考技术A ?"Insert Into room Values('"
+ jTRoomNum.getText().trim() + "','"
+ jStandard.getSelectedItem().toString() + ",'"
+ jRoomKind.getSelectedItem().toString()
+ "0,'"
+ jTARemark.getText()+ ",?,?)'") 前面你加了单引号,后面没有结束的单引号。看标出粗体,后面也一样。 参考技术B 你是sql语句有问题,我下面加星号的地方是后面对应vlaues的值,一般不要写星号.
string sql="insert into room * values('3-001','豪华间','双人间','0','null','null',' ','null')"
daoClient.queryForList(sql);
我用的是jdbc这里就不细说了 参考技术C "Insert Into room Values('"
+ jTRoomNum.getText().trim() + ",'"
+ jStandard.getSelectedItem().toString() + ",'"
+ jRoomKind.getSelectedItem().toString()
+ "0,'"
+ jTARemark.getText()+ ",?,?)'")你这个没错,但是不怎么安全。可以注入SQL语句。 参考技术D Connection conn = DriverManager.getConnection(DBC_URL, DBC_USER, DBC_PASSWORD);
Statement stm=conn.createStatement();
ResultSet rs=stm.executeQuery(sql);
返回一个结果集
在 Java 中使用插入命令运行 SQL 文件
【中文标题】在 Java 中使用插入命令运行 SQL 文件【英文标题】:Run SQL file with inset Commands in Java 【发布时间】:2012-01-24 08:06:04 【问题描述】:我想运行一个包含很多插入语句的 SQL 文件,我正在尝试运行该文件,到目前为止我找到了这段代码,但它并不好,因为它给了我一个找不到表的错误,你看看代码也许能找到问题?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
try
URL = jTextField1.getText();
username = jTextField3.getText();
password = jTextField2.getText();
String s = new String();
StringBuilder sb = new StringBuilder();
Connection conn = (Connection) DriverManager.getConnection(URL, username, password);
Class.forName("com.mysql.jdbc.Driver");
try
Statement st = conn.createStatement();
String query = "DELETE FROM player;";
st.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Delete Compleated");
catch (Exception e)
JOptionPane.showMessageDialog(this, e.getMessage());
////////////////////////////////////////////////////////////////////////////////
try
FileReader fr = new FileReader(new File("player.sql"));
// be sure to not have line starting with "--" or "/*" or any other non aplhabetical character
BufferedReader br = new BufferedReader(fr);
while ((s = br.readLine()) != null)
sb.append(s);
br.close();
// here is our splitter ! We use ";" as a delimiter for each request
// then we are sure to have well formed statements
String[] inst = sb.toString().split(";");
Statement st = conn.createStatement();
for (int i = 0; i < inst.length; i++)
// we ensure that there is no spaces before or after the request string
// in order to not execute empty statements
if (!inst[i].trim().equals(""))
st.executeUpdate(inst[i]);
System.out.println(">>" + inst[i]);
JOptionPane.showMessageDialog(this,"Loaded Compleat");
catch (Exception e)
JOptionPane.showMessageDialog(this, e.getMessage());
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
try
Statement st = conn.createStatement();
String query = "Select * from player;";
ResultSet rs = st.executeQuery(query);
while (rs.next())
int d1 = rs.getInt(1);
String d2 = rs.getString("PlayerName");
String d3 = rs.getString("PlayerSurname");
String d4 = rs.getString("PlayingPosition");
double d5 = rs.getDouble("PlayerRating");
model.addRow(new Object[]
d1, d2, d3, d4, d5
);
rs.close();
st.close();
conn.close();
catch (Exception e)
JOptionPane.showMessageDialog(this, e.getMessage());
catch (ClassNotFoundException ex)
Logger.getLogger(Football.class.getName()).log(Level.SEVERE, null, ex);
catch (SQLException ex)
Logger.getLogger(Football.class.getName()).log(Level.SEVERE, null,ex);
感谢您提供的任何帮助 :) 我正在使用 Xampp,并且数据库已准备就绪,我所需要的只是填充它,
【问题讨论】:
【参考方案1】:也许您必须在此代码中添加新行 "\n"
while ((s = br.readLine()) != null)
sb.append(s);
sb.append("\n");
因为你在前一行之后附加了每一行
【讨论】:
以上是关于java 中如何使用sql插入语句的主要内容,如果未能解决你的问题,请参考以下文章