jsp数据库操作问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 23:10:45
try{
Class.forName("com.mysql.jdbc.Driver");
String strCon="jdbc:mysql://localhost:3306/";
conn=DriverManager.getConnection(strCon,"root","19790111");
stmt=conn.createStatement();
sql="create database JspSamples";
ret=stmt.executeUpdate(sql);
sql="use JspSamples";
ret=stmt.executeUpdate(sql);
out.println("<h1>hello</h1>");
sql="CREATE TABLE customers("+
"Id int(6) unsign NOT NULL auto_increment,"+
"Name varchar(20) default NULL,"+
"addTime timestamp NULL default NULL,"+
"Tel vetchar(15) default NULL," +
"Email varchar(20) default NULL,"+
"PIMARY KEY (Id)"+
")";
ret=stmt.executeUpdate(sql);
}

这是我用jsp创建数据库的语句,print语句之前正常执行,但在执行use JspSample时抛出异常,哪位高人能帮忙解释一下原因?着急等待中....
刚才说错了

异常给出来啊,病症都没有就要开药啊。还有,用高级语言sql语句##不能##操纵到数据库级别

最多是创建表,不能创建数据库,数据库必须是已经存在的!!-

你先在外面建立数据库JspSamples,再
String strCon="jdbc:mysql://localhost:3306/JspSamples";

没见过这样写的。用数据库一般都是写在url里面的,也就是你的strCon。
String strCon="jdbc:mysql://localhost:3306/JspSamples";

如果说上面错的话,应该是这一句:ret=stmt.executeUpdate(sql),这个是更新,查询应该用executeQuery或execute。

xingjian22正解,应该写成
String strCon="jdbc:mysql://localhost:3306/JspSamples";
这样的形式。