高分求助,JAVA连数据库问题?急!再线等~~~~~~~~~~~~~~~~~~~
开发工具
windows xp2
JDK 1.4.2
eclipse 2.1
mysql 6.0
jdbc 5.0.6
请教如何将java连到mysql上,jdbc如何配置?
我现在是将mysql-connector-java-5.0.6-bin-g.jar 和aspectjrt.jar 都放到了jdk的ext里边。
又将mysql-connector-java-5.0.6-bin.jar 放到了eclipse 的java构件路径里(project-> 属性-> java build path -> libraries-> 然后点add external jars 添加)
测试代码
import java.sql.*;
public class mysql
{
public static void main(String[] args)
{
try
{
String driver = "com.mysql.jdbc.Driver ";
String url = "jdbc:myql://localhost:3306/test ";
String user = "root ";
String password = "1 ";
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
rs = stmt.executeQuery( "select * from table_1 ");
while (rs.next())
{
String name = rs.getString( "name ");
System.out.println( "name: "+name);
}
rs.close();
conn.close();
}
catch (Exception e)
{
System.out.println( "无法连接数据库 ");
e.printStackTrace();
}
}
}
错误提示:说我驱动不对,我该用什么JDBC才可以,如何配置eclipse才可以连上
Tue Jun 12 10:25:45 CST 2007 TRACE: --> NonRegisteringDriver.java:269 com.mysql.jdbc.NonRegisteringDriver.connect( "jdbc:myql://localhost:3306/test ", {user=root, password=1})
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", "jdbc:mysql:loadbalance:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", 0, "jdbc:mysql:loadbalance:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", "jdbc:mysql:replication:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", 0, "jdbc:mysql:replication:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: --> NonRegisteringDriver.java:543 com.mysql.jdbc.NonRegisteringDriver.parseURL( "jdbc:myql://localhost:3306/test ", {user=root, password=1})
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", "jdbc:mysql:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", 0, "jdbc:mysql:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", "jdbc:mysql:mxj:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", 0, "jdbc:mysql:mxj:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", "jdbc:mysql:loadbalance:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", 0, "jdbc:mysql:loadbalance:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", "jdbc:mysql:replication:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: --> StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase( "jdbc:myql://localhost:3306/test ", 0, "jdbc:mysql:replication:// ")
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1115 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- StringUtils.java:1131 com.mysql.jdbc.StringUtils.startsWithIgnoreCase(..) returning false
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- NonRegisteringDriver.java:543 com.mysql.jdbc.NonRegisteringDriver.parseURL(..) returning null
Tue Jun 12 10:25:45 CST 2007 TRACE: <-- NonRegisteringDriver.java:269 com.mysql.jdbc.NonRegisteringDriver.connect(..) returning null
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at mysql.main(mysql.java:17)
[解决办法]
MYSQL 的 驱动换比较低的版本试试吧 例如 3.1
[解决办法]
原文:String url = "jdbc:myql://localhost:3306/test ";
___________________________^__________________________
改为:String url = "jdbc:mysql://localhost:3306/test ";
___________________________@__________________________
粗心大意了,呵呵
[解决办法]
连接字是一个都不能错的,呵呵