tomcat与mysql的连接 5
用jsp做得一个系统,可是tomcat启动后,用浏览器打开页面都正常,但是登陆用户后,页面跳转错误,数据库mysql连接不起来怎么弄啊?急用...
用jsp做得一个系统,可是tomcat启动后,用浏览器打开页面都正常,但是登陆用户后,页面跳转错误,数据库mysql连接不起来怎么弄啊?急用
展开
2个回答
展开全部
首先你要有mysql-connector-java-5.1.0-bin.jar,将该文件放置在tomcat\webapps\ROOT\WEB-INF\lib目录下,确保你能正确登陆mysql,你可以直接打开mysql,先执行下你的查询语句,这里有个tomcat连接mysql的例子:
Class.forName("com.mysql.jdbc.Driver");
String dsn="jdbc:mysql://localhost:3306/test_db?user=root&password=sa";
Connection conn=DriverManager.getConnection(dsn);
Statement cmd=conn.createStatement();
//新增数据
String sql = "select max(id) as num from employee";
ResultSet numRs = cmd.executeQuery(sql);
numRs.next();
int id = numRs.getInt(1) + 1;
sql = "insert into employee(id,last_name,first_name,birth,sex,email) values(" + id + ",'cc,'dd','1982-11-16','M','abc@cc.com')";
cmd.executeUpdate(sql);
//查询数据
ResultSet rs=cmd.executeQuery("select * from employee");
while(rs.next())
{
out.println(rs.getString(1) + "--" + rs.getString(2) + "--" + rs.getString(3)+ "--" + rs.getString(4)+ "--" + rs.getString(5)+ "--" + rs.getString(6));
out.println("<br/>");
Class.forName("com.mysql.jdbc.Driver");
String dsn="jdbc:mysql://localhost:3306/test_db?user=root&password=sa";
Connection conn=DriverManager.getConnection(dsn);
Statement cmd=conn.createStatement();
//新增数据
String sql = "select max(id) as num from employee";
ResultSet numRs = cmd.executeQuery(sql);
numRs.next();
int id = numRs.getInt(1) + 1;
sql = "insert into employee(id,last_name,first_name,birth,sex,email) values(" + id + ",'cc,'dd','1982-11-16','M','abc@cc.com')";
cmd.executeUpdate(sql);
//查询数据
ResultSet rs=cmd.executeQuery("select * from employee");
while(rs.next())
{
out.println(rs.getString(1) + "--" + rs.getString(2) + "--" + rs.getString(3)+ "--" + rs.getString(4)+ "--" + rs.getString(5)+ "--" + rs.getString(6));
out.println("<br/>");
}
cmd.close();
rs.close();
conn.close();
}
需下载连接数据驱动mysql-connector-java-5.1.0-bin.jar,这个文件可到站点基地[zdbase.com]下载,下载后将mysql-connector-java-5.1.0-bin.jar放置tomcat\webapps\ROOT\WEB-INF\lib目录下即可
下载附件:mysql-connector-java-5.1.0-bin.jar
Class.forName("com.mysql.jdbc.Driver");
String dsn="jdbc:mysql://localhost:3306/test_db?user=root&password=sa";
Connection conn=DriverManager.getConnection(dsn);
Statement cmd=conn.createStatement();
//新增数据
String sql = "select max(id) as num from employee";
ResultSet numRs = cmd.executeQuery(sql);
numRs.next();
int id = numRs.getInt(1) + 1;
sql = "insert into employee(id,last_name,first_name,birth,sex,email) values(" + id + ",'cc,'dd','1982-11-16','M','abc@cc.com')";
cmd.executeUpdate(sql);
//查询数据
ResultSet rs=cmd.executeQuery("select * from employee");
while(rs.next())
{
out.println(rs.getString(1) + "--" + rs.getString(2) + "--" + rs.getString(3)+ "--" + rs.getString(4)+ "--" + rs.getString(5)+ "--" + rs.getString(6));
out.println("<br/>");
Class.forName("com.mysql.jdbc.Driver");
String dsn="jdbc:mysql://localhost:3306/test_db?user=root&password=sa";
Connection conn=DriverManager.getConnection(dsn);
Statement cmd=conn.createStatement();
//新增数据
String sql = "select max(id) as num from employee";
ResultSet numRs = cmd.executeQuery(sql);
numRs.next();
int id = numRs.getInt(1) + 1;
sql = "insert into employee(id,last_name,first_name,birth,sex,email) values(" + id + ",'cc,'dd','1982-11-16','M','abc@cc.com')";
cmd.executeUpdate(sql);
//查询数据
ResultSet rs=cmd.executeQuery("select * from employee");
while(rs.next())
{
out.println(rs.getString(1) + "--" + rs.getString(2) + "--" + rs.getString(3)+ "--" + rs.getString(4)+ "--" + rs.getString(5)+ "--" + rs.getString(6));
out.println("<br/>");
}
cmd.close();
rs.close();
conn.close();
}
需下载连接数据驱动mysql-connector-java-5.1.0-bin.jar,这个文件可到站点基地[zdbase.com]下载,下载后将mysql-connector-java-5.1.0-bin.jar放置tomcat\webapps\ROOT\WEB-INF\lib目录下即可
下载附件:mysql-connector-java-5.1.0-bin.jar
参考资料: http://www.zdbase.com/srm/srm/content/detail.aspx?OID=43C29EF7-C360-4EA5-901C-65AC85D094C8
展开全部
<%Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mysql";
Connection conn=DriverManager.getConnection(url,"root","");
Statement stmt=conn.createStatement();
String sql="select * from customer";
ResultSet rs=stmt.executeQuery(sql);%>
驱动注册灯都要写好,这个是JDBC
还有在工程里面加上 mysql的驱动jar包
String url="jdbc:mysql://localhost:3306/mysql";
Connection conn=DriverManager.getConnection(url,"root","");
Statement stmt=conn.createStatement();
String sql="select * from customer";
ResultSet rs=stmt.executeQuery(sql);%>
驱动注册灯都要写好,这个是JDBC
还有在工程里面加上 mysql的驱动jar包
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询