是自学教程里面的。JdbcUtils是一个小工具,用于完成数据库的连接。代码如下,你看看。我这个稍微和老师的有点不一样。
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils {
private static Properties props = null;
static{
try{
//fis = new FileInputStream("Jdbc.properties");
InputStream in = new FileInputStream("Jdbc.properties");
props = new Properties();
props.load(in);
}catch(IOException e){
throw new RuntimeException(e);
}
try{
Class.forName(props.getProperty("driverClassName"));
}catch(ClassNotFoundException e){
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(props.getProperty("url"),props.getProperty("username"),
props.getProperty("password"));
}
}
|