sql语法错误异常

icnyk63a  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(252)

这是我的密码

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7. public class JdbcEx2 {
  8. static final String username="root";
  9. static final String password="MyAditya16*";
  10. static final String driver="com.mysql.cj.jdbc.Driver";
  11. static final String url="jdbc:mysql://localhost/";
  12. public static void main(String[] args) {
  13. Scanner sc=new Scanner(System.in);
  14. String sql,name;
  15. int id,marks;
  16. Statement st=null;
  17. Connection cn=null;
  18. char ch;
  19. try {
  20. Class.forName(driver);
  21. cn=DriverManager.getConnection(url+"?useSSL=false",username,password);
  22. st=cn.createStatement();
  23. //sql="create database student;";
  24. sql="drop database student;";
  25. int rows=st.executeUpdate(sql);
  26. System.out.println("Database created");
  27. sql ="CREATE TABLE student (id INTEGER not NULL, name VARCHAR(255), marks INTEGER, PRIMARY KEY ( id ));";
  28. rows=st.executeUpdate("use student;"+sql);
  29. System.out.println("student table created");
  30. System.out.println("Do you want to insert some rows?");
  31. ch=sc.next().charAt(0);
  32. while (ch=='y'){
  33. System.out.println("Enter some data");
  34. sql="insert into student VALUES(?,?,?)";
  35. st=cn.prepareStatement(sql);
  36. System.out.print("\nEnter id:");
  37. id=sc.nextInt();
  38. System.out.print("\nEnter name:");
  39. name=sc.next();
  40. System.out.print("\nEnter marks:");
  41. marks=sc.nextInt();
  42. ((PreparedStatement) st).setInt(1,id);
  43. ((PreparedStatement) st).setString(2,name);
  44. ((PreparedStatement) st).setInt(3,marks);
  45. }
  46. cn.close();
  47. st.close();
  48. sc.close();
  49. } catch (SQLException e) {
  50. e.printStackTrace();
  51. } catch(Exception e){
  52. e.printStackTrace();
  53. }
  54. finally{
  55. try{
  56. if(st!=null){
  57. st.close();
  58. }
  59. }
  60. catch(SQLException e2){
  61. e2.printStackTrace();
  62. }try{
  63. if(cn!=null){
  64. cn.close();
  65. }
  66. }
  67. catch(SQLException e1){
  68. e1.printStackTrace();
  69. }
  70. }
  71. }
  72. }

它says:java.sql.sqlsyntaxerrorexception:您的sql语法有错误;检查与您的mysql服务器版本对应的手册,了解在第1行的“create table student(id integer not null,name varchar(255),marks integer,pri)”附近使用的正确语法
这个片段有什么问题

  1. sql ="CREATE TABLE student (id INTEGER not NULL, name VARCHAR(255), marks INTEGER, PRIMARY KEY ( id ));";

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题