jdbc连接数据库
package test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtil {
static String URL = null;
static String USER = null;
static String PASSWORD = null;
static{
Properties pros = new Properties();
InputStream in = JDBCUtil.class.getResourceAsStream("/jdbc.properties");
try {
pros.load(in);
} catch (IOException e) {
System.out.println("jdbc.properties文件读取异常");
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
URL = pros.getProperty("jdbcUrl");
USER = pros.getProperty("user");
PASSWORD = pros.getProperty("password");
}
public static void main(String[] args) {
Connection connection = getCon();
}
public void query(Connection conn) throws SQLException{
String sql = "select * from T_User";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet rs = preparedStatement.executeQuery();
while(rs.next()){
//转换
}
}
public boolean update(){
Connection connection=getCon();
preparedStatement = connection.prepareStatement(sql);
int index = 1;
if (params != null && !params.isEmpty()) {
for (Object object : params) {
// 使用给定对象设置指定参数的值。index从1开始
preparedStatement.setObject(index++, object);
}
}
// 在此 PreparedStatement 对象中执行 SQL 语句,该语句必须是一个 SQL 数据操作语言语句,比如
// INSERT、UPDATE 或 DELETE 语句
result = preparedStatement.executeUpdate();
}
}
public static Connection getCon(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e1) {
System.out.println("OracleDriver ClassNotFoundException");
e1.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
System.out.println("连接数据库异常");
e.printStackTrace();
}
return connection;
}
}
连接:https://www.cnblogs.com/tnt-33/p/8568140.html