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"));
System.out.println(rs.getString("password"));
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
这是运行结果
--------------------myschool------------
3
liu
123456
4
zhang
654321
这是数据库表
CREATE TABLE db_user (
id INT(20) NOT NULL AUTO_INCREMENT,
username VARCHAR(40) NOT NULL COMMENT '用户名',
PASSWORD VARCHAR(40) NOT NULL COMMENT '密码',
PRIMARY KEY (id)
) ENGINE=MYISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
还有你取的类名 text都不符合java规范 Text。且不知道你这个Text是什么意思
你写个MysqlJdbcTest也行啊