jsp中连接mysql数据库失败

问题出在Class.forName("com.mysql.jdbc.Driver") 错误提示:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver。
但是我已经在工程下添加了驱动包了,也在build path中导入了,而且在java文件测试时
一切都是正常的,但是放到JSP页面里出了问题:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

3个回答

把jar包放在工程的lib文件夹下了么?

Evankaka
Evankaka 回复得未曾有: 因为它设置自动只能去哪找jar包啊,不过,解决了就好!
接近 5 年之前 回复
Samuel__Liu
得未曾有 大发大发!!真是太谢谢了,郁闷了一天,原来是我把jar包放在了web-inf下面,能说说为什么一定要在lib下吗?是不是因为在jsp中执行只会去搜索那个目录?(在java中连接的话好像是jar包在哪都可以)(烦了一天,简直想把这各种小问题碎尸万段…)
接近 5 年之前 回复

是否二者环境不同,如果可以,断点执行找原因

 <%@ page language="java" import="com.mysql.jdbc.Driver" pageEncoding="utf-8"%>

这样试试

Samuel__Liu
得未曾有 是因为我没放在lib下。。整了一天,也是醉了
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Python+OpenCV计算机视觉

Python+OpenCV计算机视觉

jdbc连接mysql数据库驱动加载失败

各位大神帮我看看这是啥意思,加载了驱动但还是报错啊![图片说明](https://img-ask.csdn.net/upload/201511/28/1448699660_543293.png)

mysql和jsp连接问题,500错误

jsp连接数据库制作一个商品网站,登录界面输入账号密码登录500错误,每一个连接数据库的界面都是500,求大佬解答,就是下面这个c'w 登录反馈结果 com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.net.UnknownHostException MESSAGE: local STACKTRACE: java.net.UnknownHostException: local at java.net.InetAddress.getAllByName0(InetAddress.java:1157) at java.net.InetAddress.getAllByName(InetAddress.java:1083) at java.net.InetAddress.getAllByName(InetAddress.java:1019) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:246) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771) at com.mysql.jdbc.Connection.(Connection.java:1555) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:207) at myservlet.control.HandleLogin.doPost(HandleLogin.java:40) at javax.servlet.http.HttpServlet.service(HttpServlet.java:710) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:619) ** END NESTED EXCEPTION ** Last packet sent to the server was 0 ms ago.

jsp页面向mysql数据库更新数据是sql语句没错 出现异常更失败

插入信息的时候没问题 但是修改的时候异常报的是什么意思![图片说明](https://img-ask.csdn.net/upload/201712/03/1512284407_630440.png)

jsp连接数据库过程中出现SQL异常!,mysql和Tomcat也确认启动了,但还是一直报错!

代码如下: <%@ page import="java.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%> <% String url="jdbc:mysql://localhos:3306/mydatabase"; String userName="root"; String password="471414388"; Connection conn= null; String stu_id=request.getParameter("num"); Statement stmt=null; String sql=null; try{ Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException e){out.println("加载驱动器类出现异常");} try{ conn=DriverManager.getConnection(url,userName,password); stmt=conn.createStatement(); sql="select * from student"; ResultSet rs=stmt.executeQuery(sql); %> <% while(rs.next()){ int id=rs.getInt(1); String name=rs.getString(2); String address=rs.getString(3); String birthdate=rs.getString(4); %> <tr> <td><%=id%></td> <td><%=name%></td> <td><%=address%></td> <td><%=birthdate%></td> </tr> <% } rs.close(); stmt.close(); }catch(SQLException e){out.println("连接数据库过程中出现SQL异常");} finally{ try{ if(conn != null) conn.close();}catch(SQLException e){ out.println("关闭数据库连接时出现SQL异常");} }%> 已经把connector放在lib下了,但是还是一直SQL异常的错,到底应该怎么办嘤嘤嘤QAQ ![图片说明](https://img-ask.csdn.net/upload/201911/26/1574756378_185801.jpg) ![图片说明](https://img-ask.csdn.net/upload/201911/26/1574756877_818028.jpg) 很急!求救!QAQ

JSP连接数据库失败,但在主函数中编写连接数据库的代码可以连接成功,不知道是什么问题,求解答?

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 找不到服务!!

jsp链接数据库一直抛出异常?

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.sql.*"%> <html> <head><title>通过JSP读取数据库中的表</title></head> <body> <% try { Class.forName("com.mysql.jdbc.Driver"); ////加载数据库驱动 String url = "jdbc:mysql://localhost:3306/csf?serverTimezone=UTC&characterEncoding=utf-8"; //指向数据库table1 String username = "root"; //数据库用户名 String password = "1234"; //数据库用户密码 Connection conn = DriverManager.getConnection(url, username, password); //连接数据库 if(conn != null){ out.print("数据库连接成功!"); out.println("<br />"); out.println("-------------------------------"); out.print("<br />"); } else{ out.print("连接失败!"); } } catch (Exception e) { out.print("数据库连接异常!"); } %> </body> </html>

idea 无法连接数据库 mysql

Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unable to load authentication plugin 'caching_sha2_password'.) 数据库的配置等正常; 如何排除那呢?

Java Web连接MySql数据库,修改数据库连接类中的用户名和密码没有效果?

现在在做一个项目,其中编写了数据库连接及操作的类。 1、ConnDB类。 package com.wgh.tools; import java.io.InputStream; //导入java.io.InputStream类 import java.sql.*; //导入java.sql包中的所有类 import java.util.Properties; //导入java.util.Properties类 public class ConnDB { public Connection conn = null; // 声明Connection对象的实例 public Statement stmt = null; // 声明Statement对象的实例 public ResultSet rs = null; // 声明ResultSet对象的实例 //private static String propFileName = "connDB.properties"; // 指定资源文件保存的位置 //private static Properties prop = new Properties(); // 创建并实例化Properties对象的实例 private static String dbClassName = "com.mysql.jdbc.Driver"; // 定义保存数据库驱动的变量 private static String dbUrl = "jdbc:mysql://127.0.0.1:3306/db_9griddiary?user=root&password=&useUnicode=true&characterEncoding=utf8"; // public ConnDB() { // 构造方法 // try { // 捕捉异常 // // 将Properties文件读取到InputStream对象中 // InputStream in = getClass().getResourceAsStream(propFileName); // prop.load(in); // 通过输入流对象加载Properties文件 // dbClassName = prop.getProperty("DB_CLASS_NAME"); // 获取数据库驱动 // // 获取连接的URL // dbUrl = prop.getProperty("DB_URL", dbUrl); // } catch (Exception e) { // e.printStackTrace(); // 输出异常信息 // } // } /** * 功能:获取连接的语句 * * @return */ public static Connection getConnection() { Connection conn = null; try { // 连接数据库时可能发生异常因此需要捕捉该异常 Class.forName(dbClassName).newInstance(); // 装载数据库驱动 conn = DriverManager.getConnection(dbUrl); // 建立与数据库URL中定义的数据库的连接 } catch (Exception ee) { ee.printStackTrace(); // 输出异常信息 } if (conn == null) { System.err .println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" + dbClassName + "\r\n链接位置:" + dbUrl); // 在控制台上输出提示信息 } return conn; // 返回数据库连接对象 } /* * 功能:执行查询语句 */ public ResultSet executeQuery(String sql) { try { // 捕捉异常 conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(sql); } catch (SQLException ex) { System.err.println(ex.getMessage()); // 输出异常信息 } return rs; // 返回结果集对象 } /* * 功能:执行更新操作 */ public int executeUpdate(String sql) { int result = 0; // 定义保存返回值的变量 try { // 捕捉异常 conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); result = stmt.executeUpdate(sql); // 执行更新操作 } catch (SQLException ex) { result = 0; // 将保存返回值的变量赋值为0 } return result; // 返回保存返回值的变量 } /* * 功能:关闭数据库的连接 */ public void close() { try { // 捕捉异常 if (rs != null) { // 当ResultSet对象的实例rs不为空时 rs.close(); // 关闭ResultSet对象 } if (stmt != null) { // 当Statement对象的实例stmt不为空时 stmt.close(); // 关闭Statement对象 } if (conn != null) { // 当Connection对象的实例conn不为空时 conn.close(); // 关闭Connection对象 } } catch (Exception e) { e.printStackTrace(System.err); // 输出异常信息 } } } 第一次连接成功。 第二次为什么无论把这个类中的用户名和密码修改成什么都能连接上MySql了? 请问这个类中的用户名和密码有用吗? 那在哪儿修改用户名和密码后就连接不上Mysql了?

tomcat 404 提示连接不上mysql数据库????? 求各位大神帮忙,拜托

各位大神求帮忙,想要实验下复制过来的一个web项目,但是死活数据库连接不上。特地截图方便各位大神帮忙看看到底毛病出在什么地方,该怎么改正。萌萌新网上找了很多原因但无从下手= =、 附上服务器配置图,c3p0的jar包也导入了,sqlyog也能登录: ![图片说明](https://img-ask.csdn.net/upload/201812/05/1543996950_913457.png) 以下是console错误提示图: ![图片说明](https://img-ask.csdn.net/upload/201812/05/1543996976_212049.png) ![图片说明](https://img-ask.csdn.net/upload/201812/05/1543996990_107600.png) ![图片说明](https://img-ask.csdn.net/upload/201812/05/1543996998_722645.png) ![图片说明](https://img-ask.csdn.net/upload/201812/05/1543997007_319368.png) ![图片说明](https://img-ask.csdn.net/upload/201812/05/1543997016_891349.png) ![图片说明](https://img-ask.csdn.net/upload/201812/05/1543997028_305420.png)

用jsp写的表单用户登录页面,访问mysql数据库时候 出错 ,如下图

在处理登录的时候为什么有时候出现500错误,有时候又出现找不到数据库连接,我用得mysql数据库,驱动程序也添加了的,求大神指教一下啊! ![登录页面![登录处理错误页面![注册页面![注册处理错误页面](https://img-ask.csdn.net/upload/201505/20/1432088914_125244.jpg)](https://img-ask.csdn.net/upload/201505/20/1432088899_672024.jpg)](https://img-ask.csdn.net/upload/201505/20/1432088869_715910.jpg)](https://img-ask.csdn.net/upload/201505/20/1432088840_415605.jpg)

jsp中的中文数据传到mysql数据库中全部变成问号

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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> <base href="<%=basePath%>"> <title>添加学生</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <form action="StudentAction_add" method="post"> 学号:<input type="text" name="student.snumber" /><br> 姓名:<input type="text" name="student.sname" /><br> 性别:<input type="text" name="student.sex" /><br> 班级:<input type="text" name="student.sclass" /><br> 密码;<input type="text" name="student.spassword" /> <input type="submit" value="提交" /> </form> </body> </html> ``` ```<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect #hibernate.connection.driver_class com.mysql.jdbc.Driver #hibernate.connection.url jdbc:mysql:///test #hibernate.connection.username gavin #hibernate.connection.password --> <!-- 数据库驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库url --> <property name="hibernate.connection.url">jdbc:mysql:///studentmanager</property> <!-- 数据库连接用户名 --> <property name="hibernate.connection.username">root</property> <!-- 数据库连接密码 --> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- #hibernate.show_sql true #hibernate.format_sql true --> <!-- 将hibernate生成的sql语句打印到控制台 --> <property name="hibernate.show_sql">true</property> <!-- 将hibernate生成的sql语句格式化(语法缩进) --> <property name="hibernate.format_sql">true</property> <!-- ## auto schema export 自动导出表结构. 自动建表 #hibernate.hbm2ddl.auto create 自动建表.每次框架运行都会创建新的表.以前表将会被覆盖,表数据会丢失.(开发环境中测试使用) #hibernate.hbm2ddl.auto create-drop 自动建表.每次框架运行结束都会将所有表删除.(开发环境中测试使用) #hibernate.hbm2ddl.auto update(推荐使用) 自动生成表.如果已经存在不会再生成.如果表有变动.自动更新表(不会删除任何数据). #hibernate.hbm2ddl.auto validate 校验.不自动生成表.每次启动会校验数据库中表是否正确.校验失败. --> <property name="hibernate.hbm2ddl.auto">update</property> <!--可重复读--> <property name="hibernate.connection.isolation">4</property> <!-- 指定session与当前线程绑定 --> <property name="hibernate.current_session_context_class">thread</property> ``` ```

jsp调用数据库插入数据失败

往数据库插入数据总是显示空白页,错在哪儿了?下面注释掉的那段代码需要吗(加上也不行) <%@ page contentType="text/html;charset=gb2312" import="java.util.*" language="java"%> <% request.setCharacterEncoding("gb2312"); %> <%@ page import="java.io.*,java.sql.*"%> <% /*String gysname=request.getParameter("gysname"); String dm=request.getParameter("dm"); String address=request.getParameter("address"); String uzbm=request.getParameter("uzbm"); String lxr=request.getParameter("lxr"); String tel=request.getParameter("tel");*/ Connection con; String sql; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); String dbUrl = "jdbc:oracle:thin:@localhost:1521:sale";//这里要写上IP地址和oracle的那个数据库的服务名 String dbUser = "tanton"; String dbPwd = "******"; con = DriverManager.getConnection(dbUrl,dbUser,dbPwd); sql="INSERT INTO AGENCY_CONFIG(AGENCY_CODE,AGENCY_NAME,MAILING_ADDRESS,ZIP_CODE,SUPERVISOR,TELEPHONE,BANK_NAME) VALUES('03','呵呵','菏泽','274000','王老板','5555555')"; Statement stmt=con.createStatement(); PreparedStatement pst = con.prepareStatement(sql); /*pst.setString(1,dm); pst.setString(2,gysname); pst.setString(3,address); pst.setString(4,uzbm); pst.setString(5,lxr); pst.setString(6,tel);*/ pst.executeUpdate(); out.println("插入记录执行成功!"); } catch(Exception e){ e.printStackTrace();} %>

请问个jsp交互数据库插入数据失败问题

如标题,插入数据失败,但是代码没错,运行页面也不报错,网上似乎也有类似问题,但都没有明确的解答。 我不会sevlet和断点测试。通过打印语句显示测试,发现在----红色剪头处-----,一直卡在这条代码处。请大家指教。 之前用Oracle数据库也是这样的问题,然后换成MySQL还是这样。 ![图片说明](https://img-ask.csdn.net/upload/201911/25/1574664478_653711.png)![图片说明](https://img-ask.csdn.net/upload/201911/25/1574664488_254897.png)![图片说明](https://img-ask.csdn.net/upload/201911/25/1574664492_799943.png)![图片说明](https://img-ask.csdn.net/upload/201911/25/1574664500_483284.png)![图片说明](https://img-ask.csdn.net/upload/201911/25/1574664507_465447.png) 找到问题原因了,之前测试数据库连接成功没有写了个查询语句,一直没关闭,导致进程一直存在,把表锁住了,现在有了新问题:这个进程我杀不掉,重启服务器也没用,页面已经关闭了,进程还在。 ![图片说明](https://img-ask.csdn.net/upload/201911/25/1574694940_343815.png)![图片说明](https://img-ask.csdn.net/upload/201911/25/1574694947_39389.png)

jsp连接mysql时运行sql语句失败

四月 01, 2016 12:51:30 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet [jsp] in context with path [/Test] threw exception [An exception occurred processing JSP page /register.jsp at line 49 46: 47: Connection conn = DriverManager.getConnection(url); 48: Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 49: ResultSet rs = stmt.executeQuery(sql); 50: 51: out.print("房间号"); 52: out.print("|"); Stacktrace:] with root cause com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '???' at line 1 at sun.reflect.GeneratedConstructorAccessor13.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) at com.mysql.jdbc.Util.getInstance(Util.java:387) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2505) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1370) at org.apache.jsp.register_jsp._jspService(register_jsp.java:132) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

jsp操作mysql数据库的问题

我是想做一个什么事呢:就是在jsp里用statement的executeQuery(string)来向数据库增加数据,数据库为hanshuangwu,表为infor,其中只有两个属性:id、name。执行下面的代码后,显示在“****”标记的代码处有“Unknown column 'idString' in 'field list' ”错误,菜鸟求解,请高手不吝赐教啊^_^ <%@page import="javax.servlet.jsp.tagext.TryCatchFinally"%> <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030" import="java.sql.*"%> <% request.setCharacterEncoding("gb2312"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>连接数据库</title> </head> <body> <H2>连接数据库库</H2> <% int idString=1; String nameString="呵呵"; String driverClass="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/hanshuangwu"; String username="root"; String password="161006"; try{ Class.forName(driverClass); }catch(ClassNotFoundException e) { System.out.println("加载数据库驱动程序时异常(不就是失败嘛!)内容如下:"); e.printStackTrace(); } Connection con=DriverManager.getConnection(url, username, password); Statement stmt=con.createStatement(); ****int rtn=stmt.executeUpdate("insert into infor(id,name) values(idString,nameString)");**** ResultSet rs = stmt.executeQuery("select * from infor "); rs.next(); while(rs.next() ){ out.println("id:"+rs.getInt(1)+"name:"+rs.getString(2)); } rs.close(); stmt.close(); con.close(); %> </body> </html>

jsp 链接MySQL数据库使用excuteUpdate()方法出错

<%@ page language="java" import="java.sql.*,java.io.*" pageEncoding="UTF-8"%> <% String driverNmae="com.mysql.jdbc.Driver"; String username="root"; String password="123456"; String dbName="book"; String tableNmae="bookinfo"; String url="jdbc:mysql://localhost:3306/"+dbName+"?user="+username+"&password="+password; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn=DriverManager.getConnection(url); Statement stmt=conn.createStatement();//执行 SQL语句并返回它所生成结果的对象 //定义变量接收数据 String bookname=request.getParameter("name"); String publisher=request.getParameter("publisher"); String price=request.getParameter("price"); bookname=new String(bookname.getBytes("iso-8859-1"));//对输入的数据进行编码 publisher=new String(publisher.getBytes("iso-8859-1")); price=new String(price.getBytes("iso-8859-1")); if(bookname.equals("")|publisher.equals("")|price.equals("")){ response.sendRedirect("index.jsp"); }else{ String sql="insert into bookinfo(bookname,publisher,price) values("+bookname+","+publisher+","+price+")"; stmt.executeUpdate(sql);//**就是这句老是报错** out.print("数据添加成功"); } stmt.close(); conn.close(); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>连接数据库测试</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> </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: An exception occurred processing JSP page /MyJsp.jsp at line 23 20: response.sendRedirect("index.jsp"); 21: }else{ 22: String sql="insert into bookinfo(bookname,publisher,price) values("+bookname+","+publisher+","+price+")"; 23: stmt.executeUpdate(sql);**** 24: out.print("鏁版嵁娣诲姞鎴愬姛"); 25: } 26: stmt.close(); Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) root cause javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '?,清å�Žå¤§å­¦å‡ºç‰ˆç¤?,12.00)' at line 1 org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779) org.apache.jsp.MyJsp_jsp._jspService(MyJsp_jsp.java:109) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) root cause com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '?,清å�Žå¤§å­¦å‡ºç‰ˆç¤?,12.00)' at line 1 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) java.lang.reflect.Constructor.newInstance(Constructor.java:513) com.mysql.jdbc.Util.handleNewInstance(Util.java:406) com.mysql.jdbc.Util.getInstance(Util.java:381) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536) com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1564) com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1485) org.apache.jsp.MyJsp_jsp._jspService(MyJsp_jsp.java:78) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs. -------------------------------------------------------------------------------- Apache Tomcat/6.0.13

新人求解java连接mysql,向数据库添加数据,出错

package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class test{ public static void main(String[] args)throws Exception { // 加载数据库驱动,注册到驱动管理器 Class.forName("com.mysql.jdbc.Driver"); // 数据库连接字符串 String url = "jdbc:mysql://localhost:3306/test"; // 数据库用户名 String username = "root"; // 数据库密码 String password = "2443243838"; // 创建Connection连接 Connection conn = DriverManager.getConnection(url,username,password); // 判断 数据库连接是否为空 Statement st=conn.createStatement(); Scanner scanner = new Scanner(System.in); System.out.println("1,数据注册\t"); System.out.println("2,登录\t"); System.out.println("请输入你要做的操作:\t"); int n= scanner.nextInt(); if(n==1){ // 输出连接信息 System.out.println("请输入用户名字:"); String username1 = scanner.next(); System.out.println("请输入密码:"); String password1 = scanner.next(); String sql="select * from user where username='"+username1+"'"; ResultSet ra= st.executeQuery(sql); if(ra.next()){ System.out.println("用户已存在"); }else{ sql="insert into user values=('"+username1+"','"+password1+"')"; int i=st.executeUpdate(sql); if(i!=0){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); } } // 关闭数据库连接 }else if(n==2){ }else{ // 输出连接信息 System.out.println("数据库连接失败!"); } } } 1,数据注册 2,登录 请输入你要做的操作: 1 请输入用户名字: 刘备 请输入密码: 123456 Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '=('刘备','123456')' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725) at test.test.main(test.java:43)

eclipse中jsp能连接数据库但是访问不了数据库里面的表!!!

代码如下: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <center> <br><br><br> <h2>使用jdbc访问sqlserver2008数据库</h2> <hr> <table width=800 border=2 align="center"> <tr> <th>学号</th> <th>班级</th> <th>姓名</th> <th>密码</th> </tr> <% try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch(Exception q) { q.printStackTrace(); } String dbUrl = "jdbc:sqlserver://USER-20151124PC:50493;DatabaseName=Xk1"; String user="sa"; String pwd="sa"; try { Connection conn=DriverManager.getConnection(dbUrl,user,pwd); Statement stmt=conn.createStatement(); String sql="select * from Name"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()){ %> <tr> <td><%=rs.getString("Id")%></td> <td><%=rs.getString("Name") %></td> <td><%=rs.getString("Age")%></td> <td><%=rs.getString("Sex") %></td> </tr> <% } rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } %> </table> <hr> </center> </body> </html> 然后访问是这样![图片说明](https://img-ask.csdn.net/upload/201604/27/1461749419_558910.png) 帮忙纠正一下!!

通过写个类测试连接sqlserver2008数据库成功,在写jsp练习中失败

最简单的连接数据库,在写练习jsp登陆页面时,输入正确的用户名密码,跳转到失败页面。整个项目有一个警告是 Description Resource Path Location Type Classpath entry E:/ware/sqldrivers_for_vc/Microsoft SQL Server JDBC Driver 2.0/sqljdbc_2.0/chs/sqljdbc4.jar will not be exported or published. Runtime ClassNotFoundExceptions may result. AboutSql P/AboutSql Classpath Dependency Validator Message 控制台提示:java.lang.ClassNotFoundException: com.micsoft.sqlserver.jdbc.SQLServerDriver 我之前在一个类里测试了能连接数据库加载驱动。还没入门的菜鸟求教,真心累

2019 Python开发者日-培训

2019 Python开发者日-培训

150讲轻松搞定Python网络爬虫

150讲轻松搞定Python网络爬虫

设计模式(JAVA语言实现)--20种设计模式附带源码

设计模式(JAVA语言实现)--20种设计模式附带源码

YOLOv3目标检测实战:训练自己的数据集

YOLOv3目标检测实战:训练自己的数据集

java后台+微信小程序 实现完整的点餐系统

java后台+微信小程序 实现完整的点餐系统

三个项目玩转深度学习(附1G源码)

三个项目玩转深度学习(附1G源码)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

2019 AI开发者大会

2019 AI开发者大会

玩转Linux:常用命令实例指南

玩转Linux:常用命令实例指南

一学即懂的计算机视觉(第一季)

一学即懂的计算机视觉(第一季)

4小时玩转微信小程序——基础入门与微信支付实战

4小时玩转微信小程序——基础入门与微信支付实战

Git 实用技巧

Git 实用技巧

Python数据清洗实战入门

Python数据清洗实战入门

使用TensorFlow+keras快速构建图像分类模型

使用TensorFlow+keras快速构建图像分类模型

实用主义学Python(小白也容易上手的Python实用案例)

实用主义学Python(小白也容易上手的Python实用案例)

程序员的算法通关课:知己知彼(第一季)

程序员的算法通关课:知己知彼(第一季)

MySQL数据库从入门到实战应用

MySQL数据库从入门到实战应用

机器学习初学者必会的案例精讲

机器学习初学者必会的案例精讲

手把手实现Java图书管理系统(附源码)

手把手实现Java图书管理系统(附源码)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

.net core快速开发框架

.net core快速开发框架

玩转Python-Python3基础入门

玩转Python-Python3基础入门

Python数据挖掘简易入门

Python数据挖掘简易入门

微信公众平台开发入门

微信公众平台开发入门

程序员的兼职技能课

程序员的兼职技能课

Windows版YOLOv4目标检测实战:训练自己的数据集

Windows版YOLOv4目标检测实战:训练自己的数据集

HoloLens2开发入门教程

HoloLens2开发入门教程

微信小程序开发实战

微信小程序开发实战

Java8零基础入门视频教程

Java8零基础入门视频教程

相关热词 c#树形选择 c#中类图的使用方法 c# 传参 调用exe c# 怎么定义方法 c# 修改本地时间 c#前台怎么读取资源文件 c# xml转list c#实现框选截图 m*m乘法表c# c# 乘法99表
立即提问