sql语法错误异常

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

这是我的密码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class JdbcEx2 {
static final String username="root";
static final String password="MyAditya16*";
static final String driver="com.mysql.cj.jdbc.Driver";
static final String url="jdbc:mysql://localhost/";
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String sql,name;
    int id,marks;
    Statement st=null;
    Connection cn=null;
    char ch;
    try {
        Class.forName(driver);
        cn=DriverManager.getConnection(url+"?useSSL=false",username,password);
        st=cn.createStatement();
        //sql="create database student;";
        sql="drop database student;";
        int rows=st.executeUpdate(sql);
        System.out.println("Database created");
        sql ="CREATE TABLE student (id INTEGER not NULL, name VARCHAR(255), marks INTEGER, PRIMARY KEY ( id ));";
        rows=st.executeUpdate("use student;"+sql);
        System.out.println("student table created");
        System.out.println("Do you want to insert some rows?");
        ch=sc.next().charAt(0);
        while (ch=='y'){
            System.out.println("Enter some data");
            sql="insert into student VALUES(?,?,?)";
            st=cn.prepareStatement(sql);
            System.out.print("\nEnter id:");
            id=sc.nextInt();
            System.out.print("\nEnter name:");
            name=sc.next();
            System.out.print("\nEnter marks:");
            marks=sc.nextInt();
            ((PreparedStatement) st).setInt(1,id);
            ((PreparedStatement) st).setString(2,name);
            ((PreparedStatement) st).setInt(3,marks);
        }
        cn.close();
        st.close();
        sc.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try{
            if(st!=null){
                st.close();
            }
        }
        catch(SQLException e2){
            e2.printStackTrace();
        }try{
            if(cn!=null){
                cn.close();
            }
        }
        catch(SQLException e1){
            e1.printStackTrace();
        }
    }
}

}

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

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

暂无答案!

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

相关问题