急急急`````jdbc连接到数据库的问题
我只是吧jdbc解压到了一个新建的文件夹后,管理工具-数据源(odbc)-添加-SQL server-名称,描述,服务器.-默认到完成.
然后编写了<%@page language="java" contentType="text/html;charset=gb2312" page-Encoding="gb2312" %>
<%@page import="java.util.*" %>
<%@page import="java.sql.*" %>
<!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><font size=2>
<%
Connection conn=null;
Statement stmt=null;
String sql=null;
ResultSet rs=null;
int ret;
try{
Class.forName("com.SQL server.jdbc.Driver");
String strCon="jdbc:microsoft:sqlserver://localhost:1433";
conn=DriverManager.getConnection(strCon,"root","root");
stmt=conn.createStatement();
sql="create database LHX ";
ret=stmt.executeUpdate(sql);
sql="use LHX";
ret=stmt.executeUpdate(sql);
sql="create table customers("+
"ID int(6) unsigned NOT NULL auto_increment,"+
"Name varchar(20) default NUll,"+
"addTime timestamp NULL default NULL,"+
"Tel varchar(15) default NULL,"+
"Email varchar(20) default NULL,"+
"PRIMARY KEY (ID)"+
")";
ret=stmt.executeUpdate(sql);
}
catch(ClassNotFoundException e){
e.printStackTrace();
out.println("<h1>无法找到数据库驱动</h1>");
}
catch(SQLException e1){
e1.printStackTrace();
out.println("<h1>数据库操纵失败</h1>");
}
out.println("<h1>新建数据库LHX成功</h1>");
DatabaseMetaData meta=conn.getMetaData();
out.println("<br>");
out.println("连接字:"+meta.getURL());
out.println("<br>");
out.println("数据库产品:"+meta.getDatabaseProductName());
out.println("<br>");
out.println("数据库版本:"+meta.getDatabaseProductVersion());
out.println("<br>");
out.println("驱动程序:"+meta.getDriverName());
out.println("<br>");
out.println("驱动程序版本:"+meta.getDriverVersion());
stmt.close();
conn.close();
%>
</font>
</body>
</html>
出现了这个,是什么原因HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /Test5_1.jsp(1,1) Page directive has invalid attribute: page-Encoding
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:83)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:402)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:236)
org.apache.jasper.compiler.JspUtil.checkAttributes(JspUtil.java:348)
org.apache.jasper.compiler.Validator$DirectiveVisitor.visit(Validator.java:144)
org.apache.jasper.compiler.Node$PageDirective.accept(Node.java:578)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2180)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2230)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2236)
org.apache.jasper.compiler.Node$Root.accept(Node.java:485)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2180)
org.apache.jasper.compiler.Validator.validate(Validator.java:1478)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:247)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:456)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
note The full stack trace of the root cause is available in the Tomcat logs.
--------------------------------------------
Apache Tomcat/5.0.16
[解决办法]
1. Class.forName("com.SQL server.jdbc.Driver");这个有问题。。
2. String strCon="jdbc:microsoft:sqlserver://localhost:1433";这个有问题
3. <%@page language="java" contentType="text/html;charset=gb2312" page-Encoding="gb2312" %>红色部分去掉,已经设置了一次了。。。属性应该是pageEncoding吧?
4. sql="create database LHX ";
ret=stmt.executeUpdate(sql);
sql="use LHX";在这里去执行sql脚本,,有问题。。。没有这么做的。。。
[解决办法]
什么都写到一个页面上的,没有任何分层的,真的是很……。虽然我也是个菜鸟,但是我还是会使自己尽量利用分层模式来写啊!这样给自己排查错误也提供了简便!还有1楼说的真的很详细!
[解决办法]
小兄弟,别着急,出去走走,可能你自己也能解决呢,戒急躁,静下心来。
[解决办法]
楼主不要把java代码写到jsp页面中
[解决办法]
jsp只做显示用,要与业务分开
[解决办法]
把Java代码放到一个类中,与页面分开。下面是我写的一个类,比如这个类叫BaseDao,可以参考一下;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private static final String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=shopping";
private static final String USER_NAME = "sa";
private static final String PASSWORD = "sa";
protected Connection conn = null;
protected PreparedStatement ps = null;
protected ResultSet rs = null;
public Connection getConn() {
try {
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void closeAll() {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public int executeUpdate(String sql, Object[] params) {
int result = 0;
try {
ps = this.getConn().prepareStatement(sql);
if (params != null && params.length != 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
result = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
this.closeAll();
}
return result;
}
public ResultSet executeQuery(String sql, Object[] params)
throws SQLException {
ps = this.getConn().prepareStatement(sql);
if (params != null && params.length != 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
return ps.executeQuery();
}
}
[解决办法]
写个工具类,把这段代码放进去,以后连接数据库时,直接复制这个类就行,平常不用动里面代码
private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static final String url = "jdbc:sqlserver://localhost:1433;DataBaseName=company"; private static final String user = "sa"; private static final String pwd = "123@qwe"; private static Connection con = null; private static PreparedStatement pst = null; /* * 建立数据库连接 */ public static Connection getCon() { try { Class.forName(driver); con = DriverManager.getConnection(url, user, pwd); } catch (Exception e) { System.out.println("初始化错误:" + e.getMessage()); } return con; } /* * 关闭Connection */ public static void closeAll(Connection con, Statement pst, ResultSet rst) { try { if (con != null) { con.close(); } if (pst != null) { pst.close(); pst = null; } if (rst != null) { rst.close(); rst = null; } } catch (Exception e) { System.out.println(e.getMessage()); } }
[解决办法]