```package util;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
public class UPRLUtil {
private static File getClassFile(Class<?> clazz) {
URL path = clazz.getResource(clazz.getName().substring(
clazz.getName().lastIndexOf(".") + 1)
+ ".class");
if (path == null) {
String name = clazz.getName().replaceAll("[.]", "/");
path = clazz.getResource("/" + name + ".class");
}
return new File(path.getFile());
}
/**
* 获得具体某一个Class的目录
*
* @param clazz
* @return
*/
public static String getClassFilePath(Class<?> clazz) {
try {
return java.net.URLDecoder.decode(getClassFile(clazz)
.getAbsolutePath(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
private static File getClassPathFile(Class<?> clazz) {
File file = getClassFile(clazz);
for (int i = 0, count = clazz.getName().split("[.]").length; i < count; i++)
file = file.getParentFile();
if (file.getName().toUpperCase().endsWith(".JAR!")) {
file = file.getParentFile();
}
return file;
}
/**
* 获得classes的根目录
*
* @param clazz
* @return
*/
public static String getClassesPath(Class<?> clazz) {
try {
return java.net.URLDecoder.decode(getClassPathFile(clazz)
.getAbsolutePath(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
public static void main(String[] args) {
System.out.println(getClassFilePath(UPRLUtil.class));
System.out.println(getClassesPath(UPRLUtil.class));
}
}
package util;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class ConnectDB
{
public static void main(String args[])
{
try
{
runTest();
}
catch (SQLException ex)
{
for (Throwable t : ex)
t.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
/**
* Runs a test by creating a table, adding a value, showing the table contents, and removing the
* table.
*/
public static void runTest() throws SQLException, IOException
{
Connection conn = getConnection();
try
{
Statement stat = conn.createStatement();
stat.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))");
stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, MySQL World!')");
ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
if (result.next())
System.out.println(result.getString(1));
result.close();
stat.executeUpdate("DROP TABLE Greetings");
}
finally
{
conn.close();
}
}
/**
* Gets a connection from the properties specified in the file database.properties
* @return the database connection
*/
public static Connection getConnection() throws SQLException, IOException
{
Properties props=new Properties();
FileInputStream in=new FileInputStream(UPRLUtil.getClassesPath(ConnectDB.class)+"/database.properties");
props.load(in);
in.close();
String drivers=props.getProperty("jdbc.drivers");
if(drivers!=null)System.setProperty("jdbc.drivers",drivers );
String url=props.getProperty("jdbc.url");
String username=props.getProperty("jdbc.username");
String password=props.getProperty("jdbc.password");
return DriverManager.getConnection(url,username,password);
}
}
package test;
import java.io.IOException;
import java.sql.SQLException;
import model.User;
public class Test {
public static void main(String[] args) throws SQLException, IOException {
User.createTable();
// User user=new User();
// user.setName("123");
// user.setSex("1");
// user.insertUser(user);
// user.selectUser(1);
// System.out.println(user.getName());
// System.out.println(user.getSex());
// user.setId(2);
// user.setName("456");
// user.setSex("0");
// user.updateUser(user);
// user.setId(2);
// user.deleteUser(user);
}
}
求大神帮帮小菜鸟啊
你的数据库配置文件是database.properties,应该在src目录下,但是你没有。
这是拷贝的项目吗?把数据库配置文件放在src目录下就好了。
找不到你的数据库配置文件,检查一下目录,配置文件放在src下就好
大一新生都知道吧。。。。无语
把你的database.properties赋值粘贴到你的src目录下面 错误提示是找不到数据库配置文件
database.properties数据库配置文件没有,或者可以自己写数据库连接信息