连接数据库有问题

tjficcbw 2010-04-17 07:22:38
import java.io.*;
import java.sql.*;
import java.util.Properties;
public class Db {
private String driver;
private String url;
private String user;
private String password;
private Connection conn;
private Statement stm;
private ResultSet rs;
public Db(){
driver="com.mysql.jdbc.Driver";
url="jdbc:mysql://localhost:3306/test";
user="root";
password ="root";
}
public Db(String conf) {
loadProperties(conf);
setConn();
}
public Connection getConn(){
return this.conn;
}
//handle the properties file to get the informations for connection
private void loadProperties(String conf){
Properties props = new Properties();
try {
props.load(new FileInputStream(conf));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.driver = props.getProperty("driver");
this.url = props.getProperty("url");
this.user = props.getProperty("user");
this.password = props.getProperty("password");
}
//implement the Connection
private void setConn(){
try {
Class.forName(driver);
this.conn = DriverManager.getConnection(url,user,password);
} catch(ClassNotFoundException classnotfoundexception) {
classnotfoundexception.printStackTrace();
System.err.println("db: " + classnotfoundexception.getMessage());
} catch(SQLException sqlexception) {
System.err.println("db.getconn(): " + sqlexception.getMessage());
}
}
public void doInsert(String sql) {
try {
Statement statement = conn.createStatement();
int i = stm.executeUpdate(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeInset:" + sqlexception.getMessage());
}
}
public void doDelete(String sql) {
try {
stm = conn.createStatement();
int i = stm.executeUpdate(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeDelete:" + sqlexception.getMessage());
}
}
public void doUpdate(String sql) {
try {
stm = conn.createStatement();
int i = stm.executeUpdate(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeUpdate:" + sqlexception.getMessage());
}
}

public ResultSet doSelect(String sql) {
try {
stm = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stm.executeQuery(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeQuery: " + sqlexception.getMessage());
}
return rs;
}
public static void main(String[] args){
try{
Db db = new Db();
Connection conn = db.getConn();
if(conn != null && !conn.isClosed()) {
System.out.println("连接成功");
ResultSet rs = db.doSelect("select * from content");
while(rs.next()){
System.out.println(rs.getString(1)+":"+rs.getString(2)+":"+rs.getString(3));
}
rs.close();
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}

编译也通过了,就是不能输出内容,没反应,
注:我已经安好了,MYSQL,在test库里也创建好了表content,表里有三个字段,id,userv,pass,在数据库管理软件里可以正常显示表及内容,
我的驱动mysql-connector-java-5.0.8-bin.jar放在D:\jdk\lib里,环境变量classpath也包含了D:\jdk\lib;
不知为什么不能显示,望各位看看有什么问题,谢谢,
...全文
111 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
fei2008jun 2010-04-17
  • 打赏
  • 举报
回复
我显然还没达到这个水平....嘿嘿
yanghuistart 2010-04-17
  • 打赏
  • 举报
回复
试一下把mysql-connector-java-5.0.8-bin.jar拷到你工程的WEB-INF/lib目录下.
tjficcbw 2010-04-17
  • 打赏
  • 举报
回复
谢谢,结帐!
davis_rly 2010-04-17
  • 打赏
  • 举报
回复
.properties 这玩意自己还没有接触 呵呵 比较菜 但是从语言本身的角度看 改成 public Db(){
driver="com.mysql.jdbc.Driver";
url="jdbc:mysql://localhost:3306/test";
user="root";
password ="root";
setConn();
}看看吧 呵呵
tjficcbw 2010-04-17
  • 打赏
  • 举报
回复
我已经说了编译不报错,可生成class文件,运行也不报错就是没显示,可DEBUG报错报5楼的内容
龙四 2010-04-17
  • 打赏
  • 举报
回复
LZ连报错没有都不说,若是报错了就贴出stackTrace
alinchuan 2010-04-17
  • 打赏
  • 举报
回复
运行程序时 自动生成的Classpath 会覆盖环境变量里的CLASSPATH
只要把驱动所在的jar文件导入到项目库里 然后在 run ( 还是 build ?)里 选择 编译文件后
在 运行 文件 没先编译文件就运行 会出错的
wenyd222 2010-04-17
  • 打赏
  • 举报
回复
我在系统变量和用户变量里全加了
zfq642773391 2010-04-17
  • 打赏
  • 举报
回复
在main函数后面加个System.exit(0),再试一试。
tjficcbw 2010-04-17
  • 打赏
  • 举报
回复
放里了,还是不行,DUBUG 报如下错误
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:820]
tjficcbw 2010-04-17
  • 打赏
  • 举报
回复
我在系统变量和用户变量里全加了
D:\jdk\lib\mysql-connector-java-5.0.8-bin.jar
也不行
zfq642773391 2010-04-17
  • 打赏
  • 举报
回复
你试下把mysql-connector-java-5.0.8-bin.jar拷到你工程的lib目录下或者直接进入到工程里
lonefeifei 2010-04-17
  • 打赏
  • 举报
回复
\mysql-connector-java-5.1.7\src\org\gjt\mm\mysql\Driver.java
我记得得把这个设在路径里,你试试
lonefeifei 2010-04-17
  • 打赏
  • 举报
回复
mysql-connector-java-5.0.8-bin.jar你这个能解压出来不?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧