日期:2014-05-18  浏览次数:20701 次

java连接mysql出了点问题
错误信息是:in DBManager
Exception:com.mysql.jdbc.Driver
mysql的jar包(mysql-connector-java-5.1.18.zip)我存放在lib目录下面
代码是:
import java.sql.*;

public class text{
 
 
 static Connection conn=null; 
 static{
  System.out.println("in DBManager");
  String dbName="myschool";
  try{
  Class.forName("com.mysql.jdbc.Driver");
  //配置数据源
  String url="jdbc:mysql://loaclhost/myschool";
  conn=DriverManager.getConnection(url,"root","123456");
  }catch(Exception e){
  System.out.println("Exception:"+e.getMessage());
  }
 }
 public synchronized static Connection getConnection(){
Connection temp=null;
if(conn!=null){
temp=conn;
conn=null;
return temp;
}else{
return null;
}
 
}
 public synchronized static void releaseConnection(Connection con){
conn=con;
}
 
public static void closeConn(){
try{
conn.close();
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub

try {
Connection con=getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from grade");
while(rs.next())
{
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
// TODO: handle exception
}

}
 
}




------解决方案--------------------
服你了
我把你的程序拷到我机器上
运行
超低级错误
LZ
localhost
是你 "loaclhost"这样写的吗?

这是我的程序
/**
 *** Date:2011-12-5
 *** Time:下午02:14:57
 *** Author:刘泽敏
 *** Email:liuzem@kxtkx.com
 *** Package_Name:
 *** Package_Declaration:
 *** Project_NameTestMySql
 */

import java.sql.*;

public class Text {

static Connection conn = null;
static {
String dbName = "myschool";
try {
Class.forName("com.mysql.jdbc.Driver");
// 配置数据源
String url = "jdbc:mysql://localhost:3306/myschool";
conn = DriverManager.getConnection(url, "root", "root");
} catch (Exception e) {
System.out.println("Exception:" + e.getMessage());
}
System.out.println("--------------------myschool------------");
}

public synchronized static Connection getConnection() {
Connection temp = null;
if (conn != null) {
temp = conn;
conn = null;
return temp;
} else {
return null;
}

}

public synchronized static void releaseConnection(Connection con) {
conn = con;
}

public static void closeConn() {
try {
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub

try {
Connection con = getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from db_user");
while (rs.next()) {
System.out.println(rs.getString("id"));
System.out.println(rs.getString("username"));