我用java用数据源可以连接到mssql,但是连接mysql不用数据源吧,我用的是Apache,里面包含mysql,安装的是jdk1.6,提示错误是:classnotfoundexception:sun.mysql.jdbc.Driversqlexce...
我用java用数据源可以连接到mssql,但是连接mysql不用数据源吧,我用的是Apache,里面包含mysql,安装的是jdk1.6,提示错误是:
classnotfoundexception :sun.mysql.jdbc.Driver sqlexception :No suitable driver found for jdbc:mysql://localhost:3306/test
下面是我的代码,请各位帮忙看一下是什么问题
问题解决了追加50分
代码:
import java.sql.*;
public class Mysqltest
{
public static void main (String args[])
{
String urlstr="jdbc:mysql://localhost:3306/test";
Connection con;
String sql;
Statement stmt;
String name;
int no;
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("classnotfoundexception :");
System.err.print(e.getMessage());
}
try
{
con=DriverManager.getConnection(urlstr,"root","1");
stmt=con.createStatement();
//向test表中插入一条数据
sql="insert into test values(2222,'wang')";
stmt.executeUpdate(sql);
//检索test表中的所有记录并获取数据输出
sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
System.out.println("号码 姓名");
while(rs.next())
{
no=rs.getInt(1);
name=rs.getString(2);
System.out.println(no+" "+name);
}
//关闭连接
stmt.close();
con.close();
}catch(SQLException ex)
{
System.err.println("sqlexception :"+ex.getMessage());
}
}
}
展开