关于配置数据库后无法加载主类

jbomsky 发布于 2016/03/29 17:38
阅读 261
收藏 0

使用java代码实现对数据库表的增删查改

配置为oracle数据库无问题 配置为mysql数据库出现如下问题

源码如下:

package cn.ricey;


//import java.sql.Connection;


import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;


import org.junit.Test;


import cn.utils.ConnUtils;


public class Demo01 {
@Test
public void testQuery() throws Exception {
/**Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost :1521/xe";
Connection con = DriverManager.getConnection(url, "wong", "1234");


System.err.println("测试连接是否成功:" + con);
Statement st = con.createStatement();*/

Statement st=ConnUtils.getCon().createStatement();
System.err.println("数据操作对象:" + st);
String sql = "select * from stud";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.err.println(id + "," + name);


}
rs.close();
st.close();

}


@Test
public void testInsert() throws Exception {
Statement st=ConnUtils.getCon().createStatement();
Scanner sc = new Scanner(System.in);
System.err.println("请输入要写入的id");
String id = sc.nextLine();
System.err.println("请输入要写入的name");
String name = sc.nextLine();


String sql = "insert into stud values('" + name+ "','" + id  + "')";
int rows = st.executeUpdate(sql);
System.err.println("影响行数:" + rows);
st.close();




}


@Test
public void testDelete() throws Exception {
Statement st=ConnUtils.getCon().createStatement();
System.err.println("请输入要删除的id");
Scanner sc = new Scanner(System.in);
String id = sc.nextLine();


// 声明Sql insert
String sql = "delete from stud where id=" + id;
// 写入数据
int rows = st.executeUpdate(sql);
System.err.println("影响行数 :" + rows);
st.close();




}


@Test
public void testUpdate() throws Exception {
Statement st=ConnUtils.getCon().createStatement();
Scanner sc = new Scanner(System.in);
System.err.println("请输入要更新的id");
String id = sc.nextLine();
System.err.println("请输入要更新的name");
String name = sc.nextLine();
// 声明Sql insert
String sql = "update stud set name='"+ name +"' where id=" + id;
// 写入数据
int rows = st.executeUpdate(sql);
System.err.println("影响行数 :" + rows);
st.close();




}


public static void main(String[] args) throws Exception {
new Demo01();
}


public Demo01() throws Exception {


System.err.println("1.query\n2.delete\n3.update\n4.insert\n5.exit");
Scanner sc = new Scanner(System.in);
one: while (true) {
String op = sc.nextLine();
switch (op) {
case "1":
testQuery();
break;
case "2":
testDelete();
break;
case "3":
testUpdate();
break;
case "4":
testInsert();
break;
case "5":
break one;
default:
break;
}
 }
  }


}


----------------------------------------------------------------------------------------------------------------

工具类

package cn.utils;


import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;


public class ConnUtils {
private static Connection con;


static {
try {
Properties prop = new Properties();
prop.load(ClassLoader.getSystemResourceAsStream("jdbc.properties"));
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String name = prop.getProperty("name");
String pwd = prop.getProperty("pwd");


Class.forName(driver);
con = DriverManager.getConnection(url, name, pwd);
} catch (Exception e) {
throw new RuntimeException(e);
}


}
public static Connection getCon(){
return con;
}
}


----------------------------------------------------------------------------------------------------------

配置文件

#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521/xe
#name=wong
#pwd=1234


driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///qlu?characterEncoding=UTF-8
name=root
pwd=1234

加载中
0
布道牛
布道牛

首先无法加载类和数据库没关系

方法:1:99.9% 是环境变量没有配置好

jbomsky
jbomsky
这个问题已经解决了 是因为我配置文件时未关闭运行的程序导致bin文件夹下的配置文件被占用,无法更改,文件不匹配
返回顶部
顶部