作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿伙计们,我有这个错误消息,我在服务器和客户端之间连接,从 mysql 服务器选择数据并将其插入 mysql 客户端,但插入语句没有发生
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''context'.login VALUES (1,'Alamal','alamal','alamal','alamal)' at line 1
这是代码..
<%
try{
Connection con1;
Connection con2;
Class.forName("com.mysql.jdbc.Driver");
con1=(Connection)DriverManager.getConnection("jdbc:mysql://192.168.101.1:3306/context","hospital","0000");
PreparedStatement ps1=(PreparedStatement)con1.prepareStatement("SELECT * from hospital");
String str;
ResultSet rs1=ps1.executeQuery();
while(rs1.next()){
con2=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/context","root","");
con2.setAutoCommit(true);
con2.createStatement();
int id=rs1.getInt("ID");
String username=rs1.getString("UserName");
String password=rs1.getString("Password");
String hname=rs1.getString("HospitalName");
String haddress=rs1.getString("HospitalAddress");
PreparedStatement state= (PreparedStatement)con2.prepareStatement("INSERT INTO 'context'.'login' VALUES(?,?,?,?,?);");
state.setInt(1, id);
state.setString(2, username);
state.setString(3, password);
state.setString(4, hname);
state.setString(5, haddress);
state.executeUpdate();
con2.close();
}
con1.close();
} catch(Exception ex){
out.print(ex);
}
%>
最佳答案
您在编写 SQL 查询时犯了愚蠢的错误,现在它可以正常工作了。
<%
try{
Connection con1;
Connection con2;
Class.forName("com.mysql.jdbc.Driver");
con1=(Connection)DriverManager.getConnection("jdbc:mysql://192.168.101.1:3306/context","hospital","0000");
PreparedStatement ps1=(PreparedStatement)con1.prepareStatement("SELECT * from hospital");
String str;
ResultSet rs1=ps1.executeQuery();
while(rs1.next()){
con2=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/context","root","");
con2.setAutoCommit(true);
con2.createStatement();
int id=rs1.getInt("ID");
String username=rs1.getString("UserName");
String password=rs1.getString("Password");
String hname=rs1.getString("HospitalName");
String haddress=rs1.getString("HospitalAddress");
PreparedStatement state= (PreparedStatement)con2.prepareStatement("INSERT INTO `context`.`login` VALUES(?,?,?,?,?);");
state.setInt(1, id);
state.setString(2, username);
state.setString(3, password);
state.setString(4, hname);
state.setString(5, haddress);
state.executeUpdate();
con2.close();
}
con1.close();
} catch(Exception ex){
out.print(ex);
}
%>
关于mysql - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : You have an error in your SQL syntax;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37498176/
我是一名优秀的程序员,十分优秀!