Posted on 2006-06-24 22:12
i have only belief 阅读(324)
评论(2) 编辑 收藏 引用 所属分类:
JAVA(JSP)
我只是把mysql-connector-java-3.1.12-bin.jar放到%tomcat%/common/lib/下面,
然后运行出错,
错误如下:(两个写法错误一样)
java.sql.SQLException: 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 'use' at line 1
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
com.mysql.jdbc.Connection.execSQL(Connection.java:2988)
com.mysql.jdbc.Connection.execSQL(Connection.java:2917)
com.mysql.jdbc.Statement.executeQuery(Statement.java:824)
org.apache.jsp.testmysql_jsp._jspService(testmysql_jsp.java:53)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
页面连接代码如下:
第一种:
<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.sql.*" errorPage="" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登陆</title>
</head>
<body>
<table width="300" border="1">
<tr>
<td>用户名</td>
<td>密码:</td>
<td></td>
</tr>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名,根据实际情况改成你的用户名
String userName="root";
//密码,改成你的密码
String userPasswd="123456";
//数据库名,改成你的数据库名称
String dbName="shop";
//表名,更改成你的表名
String tableName="use";
//联结字符串
String url="jdbc:mysql://localhost:3306/"+dbName;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url,userName,userPasswd);
Statement statement = connection.createStatement();
String sql="SELECT * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {%>
<tr>
<td><%= rs.getInt(1) %></td>
<td><%= rs.getString(2) %></td>
</tr>
<%}
out.print("数据库操作成功,恭喜你");
rs.close();
statement.close();
connection.close();
%>
</table>
</body>
</html>
第二种:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/shop?user=root&password=123456&useUnicode=true&characterEncoding=8859_1";
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from use";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
希望那位可以帮我解决下
我现在在课程设计,今天搞了半天没有搞出来.
或者教我也可以.
我的qq:493823719
Email:ihaveonlybelief@gmail.com
谢谢!