java 连接数据库问题?

在连接数据库时出现的异常:No suitable driver found for jdbc:microsoft:sqlserver://localhost:1527;DatabaseName=userdetais
请问这是怎么回事??

7个回答

没有合适的Java与SQL的连接驱动器

lixiaozhen007
lixiaozhen007 你的数据库的名字,端口号,密码等等可能有问题,一般是默认的root,可能权限的问题
接近 2 年之前 回复

检查下driver路径是否正确

加 数据库 连接jar包了没

dbc42.jar 这个包不仅要在 eclipse 中add buildpath,还要在 tomcat 的 lib 目录上也添加一下

应该是缺少数据库依赖包,仔细检查下

主要看你架包啥的安装好了吗

第一:检查你的JDBC程序是否写的完善
第二:看是否导入了连接数据库所需要的Jar包
第三:检查你的数据库室友能正常连接使用
做好这几步,大概你就可以连接成功了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
java连接sql数据库问题
显示数据库中的students表 就是不行啊,不晓得什么问题~~~~~~rnpackage com.stu;rnrnrnimport java.awt.*;rnimport java.sql.*;rnimport javax.swing.*;rnimport java.util.*;rnimport java.awt.Event;rnrnpublic class Test1 extends JFramern rn //创建组件rn Vector rowData,columName;rn JTable jt=null;rn JScrollPane jsp=null;rn rn //定义数据库需要的组件rn Connection ct=null;rn PreparedStatement ps=null;rn ResultSet rs=null;rn rn public static void main(String[] args) rn Test1 test=new Test1();rnrn rn rn //构造函数rn public Test1()rn columName=new Vector();rn rn rn //设置列名rn rn columName.add("学号");rn columName.add("姓名");rn columName.add("性别");rn columName.add("年龄");rn columName.add("院系");rn columName.add("籍贯");rn rn rowData=new Vector();rn rn //连接数据库rn try rn //加载驱动rn rn rn Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");rn rn //得到连接rn ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=students");rn ps=ct.prepareStatement("select * from stu");rn rn //执行rn rs=ps.executeQuery();rn rn //显示rn while(rs.next())rn //定义行向量rn Vector hang=new Vector();rn //取得数据rn hang.add(rs.getString(1));rn hang.add(rs.getString(2));rn hang.add(rs.getString(3));rn hang.add(rs.getInt(4));rn hang.add(rs.getString(5));rn hang.add(rs.getString(6));rn rn //将hang添加到rowdatarn rowData.add(hang);rn rn rn rn rn catch (Exception e) rn e.printStackTrace();rn rn finallyrn //关闭资源rn try rn if(rs!=null)rn rs.close();rn rn if(ps!=null)rn ps.close();rn rn if(ct!=null)rn ct.close();rn rn catch (Exception e2) rn e2.printStackTrace();rn rn rn rn //添加组件rn jt=new JTable(rowData,columName);rn jsp=new JScrollPane(jt);rn rn this.add(jsp);rn //显示rn this.setSize(400,300);rn this.setLocation(400, 300);rn this.setVisible(true);rn this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rn rn rnrnrnrnrn然后运行就这样。。rn[img=https://img-bbs.csdn.net/upload/201305/22/1369200481_886372.png][/img]rnrn[img=https://img-bbs.csdn.net/upload/201305/22/1369200495_333400.png][/img]
java 连接oracle 数据库问题
Connection conn = null;rnClass.forName("oracle.jdbc.driver.OracleDriver");rnString url = "jdbc:oracle:thin:@172.16.2.229:1521:DBSERVER";rnString UserName="root";rnString PassWord ="root";rnconn = DriverManager.getConnection(url,UserName,PassWord);rnrnStatement stmt = conn.createStatement();rnResultSet rs = stmt.executeQuery("select * from insurance_accounts where name='胡军");rnwhile(rs.next())rnrs.getString(0);rnrnrs.close();rnconn.close();rnrnrn问题: rs.next 为false..是什么原因呢,有什么解决办法?
java 连接access数据库问题
url = System.getProperty("user.dir");rnurl = url + "\\"+"cooc.mdb";rnrnString dburl = "jdbc:odbc:driver=Microsoft Access Driver (*.mdb);DBQ='"+url+"'"; rnrn//这样连接总是出现java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 文件名无效。rnrnrn//String dburl = "jdbc:odbc:driver=Microsoft Access Driver (*.mdb);DBQ=F:\\JAVA1\\Acquisition\\cooc.mdb";rnrn// 这样连接很正常
JAVA连接Oracle数据库问题
rnimport java.sql.*;rnrn//Connection the Oracle DataBasernrnclass ConnectDataBasernrn public static void main(String[] args)rn rn Connection conn = null;rn Statement sta = null;rn ResultSet rs = null;rn tryrn rn Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();rn String url = "java:oracle:thin:@192.168.26.105:1521:sts105";rn rn conn = DriverManager.getConnection(url,"system","manager");rn rn rn catch (ClassNotFoundException e)rn rn System.out.println("Error=" + e.toString());rn rn catch (SQLException e)rn rn System.out.println("Error=" + e.toString());rn rn rn rnrnrn它为什么老是提示:rnunreported exception java.lang.InstantiationException; must be caught or declared to be thrownrn Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();rn到底是什么问题?
java 连接sqlserver数据库问题
刚学连接数据库,连了一天都没连上,郁闷。rnrnwinxp home sp2rnsqlserver 2000 个人版的已打sp4补丁rnMicrosoft SQL Server 2005 JDBC Driver 1.1rnjdk1.5rnrn下面是测试连接程序rnimport java.sql.*;rnrnpublic class DbTestrnrn Connection con;rn Statement sta;rn ResultSet rs; rn String driver;rn String url;rn String user;rn String pwd;rn rn public DbTest()rn rn driver ="com.microsoft.sqlserver.jdbc.SQLServerDriver";rn url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName = testdb";//建的一个数据库,有一个Persons表rn user = "testuser";//我自己在sqlserver设的用户和密码rn pwd = "123";rn init();rn rn rnrn public void init() rn tryrn rn Class.forName(driver);rn System.out.println("driver is ok");rn con = DriverManager.getConnection(url,user,pwd);rn System.out.println("connection is ok");rn sta = con.createStatement();rn String temp ="select * from Persons";rn rs = sta.executeQuery(temp);rn while(rs.next())rn System.out.println(rs.getString(1));rn rn rn catch(Exception e)rn rn e.printStackTrace();rn rn rn rn rn public static void main(String[] args)rn rn DbTest db = new DbTest();rn rnrnrn用eclipse运行结果如下rndriver is okrnjava.sql.SQLException: No suitable driverrn at java.sql.DriverManager.getConnection(Unknown Source)rn at java.sql.DriverManager.getConnection(Unknown Source)rn at DbTest.init(DbTest.java:28)rn at DbTest.(DbTest.java:19)rn at DbTest.main(DbTest.java:46)rnrn郁闷啊郁闷,哪位大侠拔刀相助啊rnrn谢了
java连接mysql数据库问题?
java连接数据库的时候,如果1天没有连数据库的话,第2天再连就连不上了,不管如何重连,除非重启程序,出错信息为Communication link failure: java.net.SocketException 请问这是什么问题,如何解决?
java连接MySQL数据库问题
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 'database='88' where id='1001'' at line 1nnnn代码:mystatement.executeUpdate("update student set math='"+math+"' ,english='"+english+"', computer='"+computer+"' ,database='"+database+"' "+" where id='"+ID+"'");
java 连接数据库问题
我是个初学者希望各位高手能解答rn1)我写了一个学生管理系统,主要是查询学生的信息(输入学生的名字就能显示这个学生的信息)rn2)我连接数据库成功了可是查询的时候总是出错(主要是想要sql server查询语句怎么写)!!!!rn3)我定义了一个按钮JButton (查询按钮)但点击这个按钮时就能查询你想要该学生的信息。rn4)然后定义了一个文本框JTextField jtf=new JTextField();rn5)然后我想灵活一点,如果你输入名字就能显示你想要的rnString n=this.jtf.getText().trim();//得到你输入学生的名字rnString sql="select * from stu where stuName=n";//这句话主要是把sql语句传过去,重点就是这里的n,怎么得不到我在文本框里字符串呢???????谢谢各位了!!!!!
java applet 连接数据库问题
用application能够编译成功,并能连接数据库sql,但换到applet上就运行不了?操作台出现以下错误:rnrnjava.security.cert.CertificateParsingException: no more data allowed for version 1 certificatern at sun.security.x509.X509CertInfo.parse(Unknown Source)rn at sun.security.x509.X509CertInfo.(Unknown Source)rn at sun.security.x509.X509CertImpl.parse(Unknown Source)rn at sun.security.x509.X509CertImpl.(Unknown Source)rn at sun.security.provider.X509Factory.parseX509orPKCS7Cert(Unknown Source)rn at sun.security.provider.X509Factory.engineGenerateCertificates(Unknown Source)rn at java.security.cert.CertificateFactory.generateCertificates(Unknown Source)rn at com.sun.deploy.security.WIExplorerCertStore.generateCertificate(Unknown Source)rn at com.sun.deploy.security.WIExplorerCertStore.loadCertificates(Native Method)rn at com.sun.deploy.security.WIExplorerCertStore.load(Unknown Source)rn at com.sun.deploy.security.WIExplorerCertStore.load(Unknown Source)rn at com.sun.deploy.security.TrustDecider.isAllPermissionGranted(Unknown Source)rn at sun.plugin2.applet.Plugin2ClassLoader.getPermissions(Unknown Source)rn at java.security.SecureClassLoader.getProtectionDomain(Unknown Source)rn at java.security.SecureClassLoader.defineClass(Unknown Source)rn at java.net.URLClassLoader.defineClass(Unknown Source)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn at java.lang.reflect.Method.invoke(Unknown Source)rn at sun.plugin2.applet.Plugin2ClassLoader.defineClassHelper(Unknown Source)rn at sun.plugin2.applet.Plugin2ClassLoader.access$400(Unknown Source)rn at sun.plugin2.applet.Plugin2ClassLoader$2.run(Unknown Source)rn at java.security.AccessController.doPrivileged(Native Method)rn at sun.plugin2.applet.Plugin2ClassLoader.findClassHelper(Unknown Source)rn at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)rn at java.lang.ClassLoader.loadClass(Unknown Source)rn at java.lang.ClassLoader.loadClass(Unknown Source)rn at java.lang.ClassLoader.loadClassInternal(Unknown Source)rn at java.lang.Class.forName0(Native Method)rn at java.lang.Class.forName(Unknown Source)rn at JT.myopen(JT.java:381)rn at JT.(JT.java:48)rn at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)rn at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)rn at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)rn at java.lang.reflect.Constructor.newInstance(Unknown Source)rn at java.lang.Class.newInstance0(Unknown Source)rn at java.lang.Class.newInstance(Unknown Source)rn at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)rn at java.awt.event.InvocationEvent.dispatch(Unknown Source)rn at java.awt.EventQueue.dispatchEvent(Unknown Source)rn at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)rn at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)rn at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)rn at java.awt.EventDispatchThread.pumpEvents(Unknown Source)rn at java.awt.EventDispatchThread.pumpEvents(Unknown Source)rn at java.awt.EventDispatchThread.run(Unknown Source)rnjava.lang.reflect.InvocationTargetExceptionrn at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)rn at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)rn at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)rn at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)rn at java.lang.Thread.run(Unknown Source)rnCaused by: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1434 connect,resolve)rn at java.security.AccessControlContext.checkPermission(Unknown Source)rn at java.security.AccessController.checkPermission(Unknown Source)rn at java.lang.SecurityManager.checkPermission(Unknown Source)rn at java.lang.SecurityManager.checkConnect(Unknown Source)rn at sun.plugin2.applet.Applet2SecurityManager.checkConnect(Unknown Source)rn at java.net.DatagramSocket.send(Unknown Source)rn at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(Unknown Source)rn at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)rn at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)rn at java.sql.DriverManager.getConnection(Unknown Source)rn at java.sql.DriverManager.getConnection(Unknown Source)rn at JT.myopen(JT.java:387)rn at JT.(JT.java:48)rn at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)rn at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)rn at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)rn at java.lang.reflect.Constructor.newInstance(Unknown Source)rn at java.lang.Class.newInstance0(Unknown Source)rn at java.lang.Class.newInstance(Unknown Source)rn at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)rn at java.awt.event.InvocationEvent.dispatch(Unknown Source)rn at java.awt.EventQueue.dispatchEvent(Unknown Source)rn at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)rn at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)rn at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)rn at java.awt.EventDispatchThread.pumpEvents(Unknown Source)rn at java.awt.EventDispatchThread.pumpEvents(Unknown Source)rn at java.awt.EventDispatchThread.run(Unknown Source)rn异常:java.lang.reflect.InvocationTargetExceptionrnjava.lang.NullPointerExceptionrn at sun.plugin2.applet.Plugin2Manager.findAppletJDKLevel(Unknown Source)rn at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)rn at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)rn at java.lang.Thread.run(Unknown Source)rn异常:java.lang.NullPointerExceptionrnrnrn要怎么做呢??
java 项目连接数据库问题
我们的项目是spring + struts +mysql ,项目的增删改查用的是springjdbctemplatern现在我们内部测试 现在的问题是 每天早上都会出现数据库连接不上,多试几下就可以连接上了,rn就可以正常使用了,在网上查了,说什么是mysql 8个失效什么的。数据连接池也加了。现在不知怎么解决rn报以下错误rn[code=java]** BEGIN NESTED EXCEPTION **rnrnjava.io.EOFExceptionrnrnSTACKTRACE:rnrnjava.io.EOFExceptionrn at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1956)rn at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2368)rn at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867)rn at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)rn at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)rn at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)rn at com.mysql.jdbc.Connection.execSQL(Connection.java:3178)rn at com.mysql.jdbc.Statement.executeQuery(Statement.java:1203)rn at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)rn at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:443)rn at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:396)rn at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:458)rn at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:466)rn at com.chinatsp.system.dao.impl.LoginDaoImpl.validUser(LoginDaoImpl.java:39)rn at com.chinatsp.system.service.impl.LoginServiceImpl.validUser(LoginServiceImpl.java:65)rn at sun.reflect.GeneratedMethodAccessor112.invoke(Unknown Source)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)rn at java.lang.reflect.Method.invoke(Method.java:601)rn at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)rn at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)rn at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)rn at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)rn at $Proxy9.validUser(Unknown Source)rn at com.chinatsp.system.action.LoginAction.login(LoginAction.java:75)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)rn at java.lang.reflect.Method.invoke(Method.java:601)rn at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)rn at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)rn at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn[/code]rnspring的配置文件如下rn[code=java]?xml version="1.0" encoding="UTF-8"?>rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn[/code]rn
Java登陆界面连接数据库问题
登陆界面连接了数据库,但总是提示秘密错误,和数据库中的用户名密码对照过了,并没有错,求大神指点迷津rnBLogin.addActionListener(new ActionListener()rn public void actionPerformed(ActionEvent e)rn String Apassword=LenPassWord.getText().trim();rn String AUserID=LenUserID.getText().trim();rn if(AUserID.equals("admin")&&Apassword.equals("123456"))rn new ChooseFountion().init();rn jf.dispose();rn elsern Connection conn=null;rn PreparedStatement pstmt=null;rn ResultSet rs=null;rn String sql="select UserPassword from [user] where UserName='"+AUserID+"'";rn tryrn conn=DBConnection.getConnection();rn pstmt=conn.prepareStatement(sql);rn rs=pstmt.executeQuery();rn if(rs.next())rn if(rs.getString("UserPassword").equals(Apassword))rn new UserChooseFountion().init();rn elsern JOptionPane.showMessageDialog(jf, "密码错误,请重新输入!",rn "登陆失败",JOptionPane.ERROR_MESSAGE);rn rn elsern JOptionPane.showMessageDialog(jf, "用户名不存在,请重新输入",rn "登陆失败",JOptionPane.ERROR_MESSAGE);rn rn rs.close();rn catch(SQLException e1)rn e1.printStackTrace();rn catch(Exception e1)rn e1.printStackTrace();rn finallyrn try rn DBConnection.close(rs,pstmt,conn);rn catch (Exception e1) rn e1.printStackTrace();rn rn rn rn rn );
java连接mysql数据库问题
java项目在linux系统上部署,启动tomcat时报错 。rn 2012-09-25 14:26:10,375 INFO [DefaultListableBeanFactory.java-414]: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@813bc1: defining beans [dataSource,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,txAdvice,baseService,sessionFactory,hibernateDao,adminService,logsService,smsAccountService,rateService,prestoreSMSService,sendSMSService,udpService,sysSetService,adminActionBean,smsAccountBean,rateBean,sendSMSBean,logsBean,sysSetBean]; root of factory hierarchyrn2012-09-25 14:26:11,360 INFO [Environment.java-543]: Hibernate 3.3.1.GArn2012-09-25 14:26:11,372 INFO [Environment.java-576]: hibernate.properties not foundrn2012-09-25 14:26:11,383 INFO [Environment.java-709]: Bytecode provider name : javassistrn2012-09-25 14:26:11,398 INFO [Environment.java-627]: using JDK 1.4 java.sql.Timestamp handlingrn2012-09-25 14:26:12,033 INFO [HbmBinder.java-322]: Mapping class: com.adt.entity.PrestoreSMS -> t_msm_prestorern2012-09-25 14:26:12,354 INFO [HbmBinder.java-322]: Mapping class: com.adt.entity.SysSet -> t_system_setrn2012-09-25 14:26:12,445 INFO [HbmBinder.java-322]: Mapping class: com.adt.entity.SendSMS -> t_historyrn2012-09-25 14:26:12,514 INFO [HbmBinder.java-322]: Mapping class: com.adt.entity.Admin -> t_sms_adminrn2012-09-25 14:26:12,584 INFO [HbmBinder.java-322]: Mapping class: com.adt.entity.Logs -> t_logrn2012-09-25 14:26:12,668 INFO [HbmBinder.java-322]: Mapping class: com.adt.entity.SMSaccount -> t_sms_accountrn2012-09-25 14:26:12,750 INFO [HbmBinder.java-322]: Mapping class: com.adt.entity.Rate -> t_ratern2012-09-25 14:26:12,754 INFO [LocalSessionFactoryBean.java-795]: Building new Hibernate SessionFactoryrn2012-09-25 14:26:12,760 INFO [HbmBinder.java-2404]: Mapping collection: com.adt.entity.SMSaccount.accountSet -> t_sms_accountrn2012-09-25 14:26:12,790 INFO [ConnectionProviderFactory.java-95]: Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProviderrn2012-09-25 14:26:13,136 WARN [JDBCExceptionReporter.java-100]: SQL Error: 0, SQLState: nullrn2012-09-25 14:26:13,140 ERROR [JDBCExceptionReporter.java-101]: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'127.0.0.1' (using password: YES))rn2012-09-25 14:26:13,147 WARN [SettingsFactory.java-144]: Could not obtain connection metadatarnorg.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'127.0.0.1' (using password: YES))rn at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)rn at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)rn at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)rn at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:111)rnCaused by: java.sql.SQLException: Access denied for user 'root'@'127.0.0.1' (using password: YES)rn at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)rn at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:770)rn at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3641)rn at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1175)rn at com.mysql.jdbc.Connection.createNewIO(Connection.java:2544)rn at com.mysql.jdbc.Connection.(Connection.java:1474)rn at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:264)rn at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)rn at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)rn at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)rn at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)rn ... 74 morern
java属性文件连接数据库问题
package db.com;rnimport java.util.Properties;rnimport java.io.*;rnrnpublic class Env extends Propertiesrn private static Env instance;rn public static Env getInstance()rn if(instance!=null)rn return instance;rn elsern makeInstance();rn return instance;rn rn rn private static synchronized void makeInstance()rn if(instance==null)rn instance=new Env();rn rn rn private Env()rn InputStream is=getClass().getResourceAsStream("/ddb.properties");rn try rn load(is);rn catch (IOException e) rn System.err.println("错误:没有读取属性文件,"+"请确认是否存在");rn rn rnrnrn==========================================================================================================rnpackage db.com;rnrnimport java.sql.Connection;rnimport java.sql.DriverManager;rnimport java.sql.SQLException;rnrnpublic class ConnectionManager rn public static synchronized Connection getConnection()rn String driverClassName=Env.getInstance().getProperty("driver");rn String url=Env.getInstance().getProperty("url");rn String user=Env.getInstance().getProperty("user");rn String password=Env.getInstance().getProperty("password");rn Connection con=null;rn try rn Class.forName(driverClassName);rn con=DriverManager.getConnection(url,user,password);rn catch (ClassNotFoundException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn catch (SQLException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn return con;rn rnrn===================================================================================================rnpackage db.com;rnrnpublic class test rnrn /**rn * @param argsrn */rn public static void main(String[] args)rn ConnectionManager.getConnection();rn System.out.println("con.getConn()");rn rn rn======================================================================================rnException in thread "main" java.lang.NullPointerExceptionrn at java.util.Properties$LineReader.readLine(Properties.java:418)rn at java.util.Properties.load0(Properties.java:337)rn at java.util.Properties.load(Properties.java:325)rn at db.com.Env.(Env.java:23)rn at db.com.Env.makeInstance(Env.java:17)rn at db.com.Env.getInstance(Env.java:11)rn at db.com.ConnectionManager.getConnection(ConnectionManager.java:9)rn at db.com.test.main(test.java:9)rn晕死了
java web连接数据库问题
在java web中欢迎index.jsp中内添加这段代码:rn <%rn tryrn Class.forName("com.mysql.jdbc.Drive");rn String url = "jdbc:mysql://localhost:3306/test";rn String username = "root";rn String password = "123";rn Connection conn = DriverManager.getConnection(url,username,password);rn if (conn != null)rn out.println("数据库连接成功!");rn conn.close();rn elsern out.println("数据库连接失败");rn rn catch(ClassNotFoundException e)rn e.printStackTrace();rn catch(SQLException e)rn rn rn %>rn但抛出如下异常,怎么回事啊:rnrnjava.lang.ClassNotFoundException: com.mysql.jdbc.Drivern at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)rn at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)rn at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)rn at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)rn at java.lang.ClassLoader.loadClassInternal(Unknown Source)rn at java.lang.Class.forName0(Native Method)rn at java.lang.Class.forName(Unknown Source)rn at org.apache.jsp.index_jsp._jspService(index_jsp.java:92)rn at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)rn at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)rn at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)rn at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)rn at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)rn at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)rn at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)rn at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)rn at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)rn at java.lang.Thread.run(Unknown Source)rn求帮助啊!
初学java,连接数据库问题
![图片说明](https://img-ask.csdn.net/upload/201511/12/1447315019_637583.png)n![图片说明](https://img-ask.csdn.net/upload/201511/12/1447315030_944496.png)n![图片说明](https://img-ask.csdn.net/upload/201511/12/1447314948_997885.png)n![图片说明](https://img-ask.csdn.net/upload/201511/12/1447314957_44591.png)
java连接Access数据库问题
[code=java]rn public Connection getConnection()rn rn String url="jdbc:odbc:driver=MicroSoft Access Driver (*.mdb);DBQ=D:\\xxx.mdb" ;rn String user = "";rn String password = "1234";rn try rn Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");rn catch (java.lang.ClassNotFoundException e) rn rn try rn con = DriverManager.getConnection(url,user,password);rn catch (SQLException ex) rn rn return con;rn rn[/code]rnAccess 的数据库连接rn设置了密码就会报错,去掉密码就能连接成功。rn求大神解答为什么?
java jdbc连接数据库问题
import java.sql.Connection;rnimport java.sql.DriverManager;rnimport java.sql.ResultSet;rnimport java.sql.Statement;rnimport java.util.Scanner;rnrnpublic class work rn public static void main(String[] args) throws Exception// (Statement)rn Class.forName("com.mysql.jdbc.Driver");rn Connection con=DriverManager.getConnection("jdbc:mysql//127.0.0.1:3306/lianxi?characterEncoding=gb2312","root","liuxu1993");rn Statement st=con.createStatement();rn Scanner sc= new Scanner (System.in);rn System.out.println("xuehao");rn String xuehao=sc.next();rn System.out.println("xingming");rn String xingming=sc.next();rn System.out.println("xingbie");rn String xingbie=sc.next();rn System.out.println("chengji");rn int chengji=sc.nextInt();rn String sql="select * from student where='"+xuehao+"'";rn ResultSet rs=st.executeQuery(sql);rn rs.next();rn if(rs.next())System.out.println("xuehaocunzai");rn else sql="insert into student values('"+xuehao+"','"+xingming+"','"+xingbie+"','"+chengji+"')";rn int i=st.executeUpdate(sql);rn if(i!=0)System.out.println("yes");rn elseSystem.out.println("no");rn rnrn运行后 显示rnjava.lang.NoSuchMethodError: mainrnException in thread "main" rn这是怎么回事
java连接access数据库问题
[code=Java][/code]rntryrn rn theStatement=theConnection.createStatement();rn theResult=theStatement.executeQuery("select * from people");rn DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();rn tableModel.setRowCount(0);rn while(theResult.next())rn rn String colValue[]=theResult.getString(1),gbk_str.gbk_string(theResult.getBytes(2)),gbk_str.gbk_string(theResult.getBytes(3));rn tableModel.addRow(colValue);rn rn jTable1.invalidate();rn rn catch(Exception e)rn rn handleException(e);rn rn这个是可以的rn[code=Java][/code]rnString peo_name=jTextField2.getText();rn tryrn rn theStatement=theConnection.createStatement();rn theResult=theStatement.executeQuery("select * from people where name ='"+peo_name+"'");rn这个就有问题了rn错误报告rn[Microsoft][ODBC Microsoft Access Driver] ?ַ???﷨???? ?ڲ?ѯ???ʽ 'name ='赵文??' ?С?rnjava.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] ?ַ???﷨???? ?ڲ?ѯ???ʽ 'name ='赵文??' ?С?rn at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)rn at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)rn at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3109)rn at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)rn at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:252)rn at MainJFrame.jButton2ActionPerformed(MainJFrame.java:185)rn at MainJFrame.access$100(MainJFrame.java:21)rn at MainJFrame$3.actionPerformed(MainJFrame.java:94)rn at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)rn at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)rn at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)rn at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)rn at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)rn at java.awt.Component.processMouseEvent(Component.java:6437)rn at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)rn at java.awt.Component.processEvent(Component.java:6202)rn at java.awt.Container.processEvent(Container.java:2213)rn at java.awt.Component.dispatchEventImpl(Component.java:4793)rn at java.awt.Container.dispatchEventImpl(Container.java:2271)rn at java.awt.Component.dispatchEvent(Component.java:4619)rn at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4816)rn at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4476)rn at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4406)rn at java.awt.Container.dispatchEventImpl(Container.java:2257)rn at java.awt.Window.dispatchEventImpl(Window.java:2677)rn at java.awt.Component.dispatchEvent(Component.java:4619)rn at java.awt.EventQueue.dispatchEvent(EventQueue.java:662)rn at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)rn at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)rn at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)rn at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)rn at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)rn at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)rn这是什么情况呢?
java 连接 mysql数据库问题!!!!!
SELECT Subject,Title FROM Books WHERE Subject LIKE '%13%'rnrn----在dos下进入mysql运行没有问题,怎么在程序中的时候出错??当改为:rnSELECT Subject,Title FROM Books WHERE Subject = '13' 也没有错误,是否like出现问题?在java程序中使用是否要注意什么?rnrnrn求助大家帮帮忙!!rnrn :)
java连接sybase数据库问题
使用java连接sybase数据库时,为何如果sybase数据库的IP地址写的正确,数据库名写错了,会自动连接到所使用用户默认的数据库上去,如何能使其不连接到用户默认的数据库,而是报告连接错误。
java连接DB2数据库问题
连接DB2数据库,执行sql后产生下列错误rnjava.sql.SQLException: [IBM][CLI Driver][DB2/NT] SQL0204N "DEMOTEST.DEMOTEST" is an undefined name. SQLSTATE=42704rnrnselect * from demotest.demotestrn在command Center 中执行没有问题啊rn在程序中就产生了上面的错误
java 连接远程数据库问题
java 连接本地mssql数据库的时候rnconn=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=xhjt", "sa", "maojw923")rn如果现在要连接远程的数据库话,看网上的资料是将IP+机器名称rnconn=DriverManager.getConnection("jdbc:microsoft:sqlserver://ip/20111028-1120:1433;DatabaseName=xhjt", "sa", "maojw923")rnrnip是我公司的IP,20111028-1120是我的电脑名称,但是这样不能连接到数据库中rn
java 连接 oracle数据库问题
java 连接oracle库 查询数据,有个别生僻中文读出来 就是问号(?),不知道什么原因。rnrn我在本地查询读取的时候中文能正常显示,但是放在服务器上运行,就会出现生僻中文乱码显示的问题。rnrn那位大虾知道是什么原因么?
Java连接oracle 10g数据库问题
出现问题:rn java.sql.SQLException: Io 异常: The Network Adapter could not establish the connectionrnrn//代码如下:rnpackage edu.mju.Test;rnimport java.sql.Connection;rnimport java.sql.DriverManager;rnimport java.sql.SQLException;rnpublic class TestOracle rn private static String DBDRIVER="oracle.jdbc.driver.OracleDriver";rn private static String DBURL="jdbc:oracle:thin:@127.0.0.1:1512:jonde";rn private static String DBUSER="scott";rn private static String DBPASSWORD="zhangde";rn private static Connection conn=null;rn public static void main(String[] args) rn try rn Class.forName(DBDRIVER);rn //连接时必须填写用户名及密码rn conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);rn System.out.println(conn);rn catch (ClassNotFoundException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn catch (SQLException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn
java 连接oracle8i 数据库问题。急~~~~~~~~~~~~~~
请问,JDBC驱动怎么设置?rnclasses12.zip,应该放在哪里?
Java 连接JDBC MySQL数据库问题
import java.sql.*; public class Main {public static void main(String[] args) { // 1.注册驱动 Connection conn = null; Statement statement = null; ResultSet res = null;
JDBC 连接 Access2000 数据库问题-java
请问,怎样实现一个list窗口与数据库的ID连接然后改变ID,这个ID对应的信息则被显示在text窗口中。例如,只选择客户的ID,则客户的地址,电话,姓名等等都要从数据库中调出来显示。rnrn在JBuilder9中。
eclipse java连接oracle数据库问题
import java.sql.Connection;rnimport java.sql.DriverManager;rnimport java.sql.ResultSet;rnimport java.sql.Statement;rnpublic class TEST rnrn public static String Find(String a)rn rn String result="";rn int B_id = Integer.parseInt(a);rn rn tryrn rn System.out.println("1");rn Class.forName("oracle.jdbc.driver.OracleDriver");rn System.out.println("2");rn Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@10.32.20.105:1521:orcl","system","admin");rn System.out.println("3");rn Statement stmt = conn.createStatement();rn System.out.println("4");rn ResultSet rs = stmt.executeQuery("select id,name from B_org where id='"+B_id+"'");rn System.out.println("5");rn while(rs.next())rn rn int b = rs.getInt("id");rn //int b = rs.getInt("org_parent_id");rn //String x = Integer.toString(a);rn String y = Integer.toString(b);rn String z = rs.getString("name");rn result =y+" "+z;rn //String result =rs.getInt(org_level)+" "+rs.getString(org_name)+" "+rs.getInt(org_prent_id); rn System.out.print(result);rn rn rn rs.close(); rn stmt.close(); rn conn.close();rn rn catch(Exception e)rn rn System.out.println("wrong"); rn rn return result;rn rn public static void main(String args[])rn rn int b = 1;rn System.out.println(TEST.Find(Integer.toString(b)));rn rnrn========================================================================rn运行结果是1 2 3 4 wrongrn我是初学者,本想输出数据表中数据列,求助高手解决问题。
java 连接MySQL数据库问题集锦
导入jar包 要通过JDBC连接MySQL数据库,需要下面导入jar包,构建开发环境 官方jar包下载地址:https://dev.mysql.com/downloads/connector/j/ 构建数据库连接URL 格式:JDBC协议+IP地址或域名+端口+数据库名称,例如:jdbc:mysql://localhost:3306/practice tip: 端口号一般为3306,如果需要查询端...
java连接Oracle 10g数据库问题
我想测试与数据库的连接是否成功,编译是没有错误的,运行时连接不成功的,该如何去做? 错误原因如下:rnjava.sql.SQLException:IO异常:the Network Adapter could not establish the connectionrnat oracle.jdbc.driver.DatabaseError.throwSqlExceptionrnat oracle.jdbc.driver.DatabaseError.throwSqlExceptionrnat oracle.jdbc.driver.DatabaseError.throwSqlExceptionrnat oracle.jdbc.driver.T4CConnection.logonrnat oracle.jdbc.driver.T4CDriverExtension.getConnectionrnat oracle.jdbc.driver.OracleDriver.connectrnat java.sql.DriverManager.getConnectionrnat java.sql.DriverManager.getConnectionrn代码如下:rnimport java.sql.*;rnpublic class linkOraclernrn public static void main(String[] args)rn rn Connection con=null;rn String url="jdbc:oracle:thin:@localhost:1521:orcl";rn String user="system";rn String password="qq";rn tryrn rn rn Class.forName("oracle.jdbc.driver.OracleDriver");rn con=DriverManager.getConnection(url,user,password);rn Statement stat=con.createStatement();rn ResultSet rs=stat.executeQuery("SELECT * FROM music");rn while(rs.next())rn rn String mid=rs.getString(1);rn String mname=rs.getString(2);rn String mcountry=rs.getString(3);rn System.out.println(mid+"\t\t"+mname+"\t\t"+mcountry);rn rn rs.close();rn stat.close();rn rn catch(Exception e)rn rn e.printStackTrace();rn rn finallyrn rn tryrn rn con.close();rn rn catch(Exception e)rn rn e.printStackTrace();rn rn rn rnrn望高手给些指点,是不是哪处的设置有问题,不胜感激!!!
JAVA连接SQL SERVER数据库问题
活见鬼了,就是连接不上,请高手指教.rn我使用的是JDK 1.5,连接SQL SERVER2000。rn报错内容是:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.rn连接程序:rnClass.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); rnconn=DriverManager.getConnection ("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=YanBuFangAn ","sa","godblessme");rn我作了以下检查和部署:rn1、安装SP4补丁rn2、确认端口号是1433rn3、确认SQL SERVER的登录方式是“SQL SERVER和WINDOWS方式”rn4、连接驱动包成功加载rn5、程序中连接关键字正确rn结果还是不行,请高手指点。是不是SP3和SP4补丁包要同时安装?本机连接与防火墙就没关系了吧?
java 纯JDBC连接数据库问题
package com.tamptest;rnrnimport java.sql.*;rnpublic class test1 rnrn /**rn * @param argsrn */rn public static void main(String[] args) rn // TODO Auto-generated method stubrn PreparedStatement psPreparedStatement=null;rn Connection ct=null;rn ResultSet rSet=null;rn rn try rn Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载驱动rn ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=examination","sa","111");rn psPreparedStatement=ct.prepareStatement("select * from exam_paper from examination");rn rSet=psPreparedStatement.executeQuery();//insert,delete,update用executeUpdate(),select用executeQuery()rn while (rSet.next()) rn int numOne=rSet.getInt(1);rn System.out.println(numOne);rn rn catch (Exception e) rn // TODO: handle exceptionrn e.printStackTrace();rn rn rnrnrnrnrnrn报的错误是rnjava.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=examinationrn at java.sql.DriverManager.getConnection(DriverManager.java:602)rn at java.sql.DriverManager.getConnection(DriverManager.java:185)rn at com.tamptest.test1.main(test1.java:17)rnrnjdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=examinationrnjdbc:microsoft:sqlserver://localhost:1433;databaseName=examinationrnjdbc:microsoft:sqlserver:服务器名字:1433;databaseName=examinationrn这三行都试过了。rnrnsqljdbc_2.0里面的两个sqljdbc4.jar和sqljdbc.jar两个包都引了rnrn求解决方案rn
连接数据库问题
package com.mwq;rnrnimport java.sql.CallableStatement;rnimport java.sql.Connection;rnimport java.sql.DriverManager;rnimport java.sql.PreparedStatement;rnimport java.sql.ResultSet;rnimport java.sql.SQLException;rnimport java.sql.Statement;rnimport java.util.ArrayList;rnimport java.util.List;rnrnpublic class TestInsert rnrn private static final String DRIVERCLASS = "com.microsoft.jdbc.sqlserver.SQLServerDriver";rnrn private final String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db_database12";rnrn private final String username = "sa";rnrn private final String password = "123";rnrn private Connection conn;rnrn private Statement stmt;rnrn private PreparedStatement prpdStmt;rnrn private CallableStatement cablStmt;rnrn static rn try rn Class.forName(DRIVERCLASS);rn catch (ClassNotFoundException e) rn System.out.println("------ 在加载数据库驱动时抛出异常,内容如下:");rn e.printStackTrace();rn rn rnrn // 执行静态INSERT语句rn public void insertOne(String name) throws SQLException rn conn = DriverManager.getConnection(url, username, password);rn stmt = conn.createStatement();rn stmt.executeUpdate("insert into tb_testInsert(name) values('" + namern + "')");// 执行静态INSERT语句rn stmt.close();rn conn.close();rn rnrn // 执行动态INSERT语句rn public void insertMore(String[] names) throws SQLException rn conn = DriverManager.getConnection(url, username, password);rn prpdStmt = connrn .prepareStatement("insert into tb_testInsert(name) values(?)");// 预处理动态INSERT语句rn prpdStmt.clearBatch();// 清除Batchrn for (int i = 0; i < names.length; i++) rn prpdStmt.setString(1, names[i]);// 为动态SQL语句赋值rn prpdStmt.addBatch();// 向Batch中添加INSERT语句rn rn prpdStmt.executeBatch(); // 批量执行Batch中的INSERT语句rn prpdStmt.close();rn conn.close();rn rnrn // 通过存储过程执行INSERT语句rn public void insertProcedure(String[] names) throws SQLException rn conn = DriverManager.getConnection(url, username, password);rn cablStmt = conn.prepareCall("call test_insert(?)");// 调用存储过程rn cablStmt.clearBatch();rn for (int i = 0; i < names.length; i++) rn cablStmt.setString(1, names[i]);rn cablStmt.addBatch();rn rn cablStmt.executeBatch();rn cablStmt.close();rn conn.close();rn rnrn // 查询所有记录rn public List selectAll() rn List list = new ArrayList();rn try rn conn = DriverManager.getConnection(url, username, password);rn stmt = conn.createStatement();rn ResultSet rs = stmt.executeQuery("select * from tb_testInsert");rn while (rs.next()) rn Object note[] = new Object[2];rn for (int i = 0; i < note.length; i++) rn note[i] = rs.getObject(i + 1);rn rn list.add(note);rn rn stmt.close();rn conn.close();rn catch (SQLException e) rn System.out.println("------ 在检索记录时抛出异常,内容如下:");rn e.printStackTrace();rn rn return list;rn rnrnrnrn我有这样的一段连接数据库代码,但里面没有main()方法,我该怎么运行,怎么看连接成功。。rn不要笑我,刚学,什么都不懂,请高手帮忙
连接数据库问题.
import javax.swing.*;rnimport java.awt.*;rnimport java.awt.event.*;rnimport java.sql.*;rnpublic class Login rn JFrame frame;rn JPanel panel;rn JLabel lblName,lblPwd;rn JTextField txtName;rn JPasswordField txtPwd;rn JButton btnLogin,btnCancel;rn GridLayout fr;rnrn public Login()rn frame=new JFrame("login form");rn panel=new JPanel();rn lblName=new JLabel("userName:");rn lblPwd=new JLabel("Password:");rn txtName=new JTextField(15);rn txtPwd=new JPasswordField(15);rn btnLogin=new JButton("login");rn btnCancel=new JButton("Cancel");rn fr= new GridLayout(3,2);rn rn rn panel.setLayout(new GridLayout(3,2));rn panel.add(lblName);rn panel.add(txtName);rn panel.add(lblPwd);rn panel.add(txtPwd);rn panel.add(btnLogin);rn panel.add(btnCancel);rn rn MyListener mylist=new MyListener();rn btnLogin.addActionListener(mylist);rn btnCancel.addActionListener(mylist);rn rn rn frame.getContentPane().add(panel);rn frame.setSize(300, 200);rn frame.setVisible(true);rn rn //注册监听器rn class MyListener implements ActionListenerrn public void actionPerformed(ActionEvent evt)rn Object obj=evt.getSource();rn if(obj==btnLogin)rn //获取文本数据转换成string rn String name=txtName.getText().trim();rn String Pwd=String.valueOf(txtPwd.getPassword()).trim();rn try rn //加载驱动程序rn Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");rn //建立数据库连接rn Connection con=DriverManager.getConnection("jdbc:odbc:mydb","sa","");rn //创建SQL语句 并发性 rn PreparedStatement pstmt=con.prepareStatement("select * from user where user_id=? and pwd=?");rnrn pstmt.setObject(1, "dutu");rn pstmt.setObject(2, 12312);rn if(name.equals(pstmt.getString(1).trim())&&Pwd.equals(pstmt.getString(2)))rn rn JOptionPane.showMessageDialog(null,"login success");rn return;rn rn rn //同上rn JOptionPane.showMessageDialog(null, "login failed");rn //关闭流rn con.close();rn rn rn //抛出异常rn catch(ClassNotFoundException e)rn System.out.println(e.getMessage());rn rn catch(SQLException e)rn System.out.println(e.getMessage());rn rn rn rn //清空文本框rn if(obj==btnCancel)rn txtName.setText("");rn txtPwd.setText("");rn rn rn rn rn public static void main(String []s)rn new Login();rn rn rnrnrnrnrnrnif(name.equals(pstmt.getString(1).trim())&&Pwd.equals(pstmt.getString(2)))rnrn我已建好了数据库.高手们看看 帮我改下.rn
相关热词 c# 标准差 计算 c#siki第五季 c#入门推荐书 c# 解码海康数据流 c# xml的遍历循环 c# 取 查看源码没有的 c#解决高并发 委托 c#日期转化为字符串 c# 显示问号 c# 字典对象池