jdbc連線資料庫的優化和防止注入
阿新 • • 發佈:2019-02-09
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class JDBCDAO {
static Connection conn =null;
@Test
private static Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
try{
String user="root";
String password="";
String url="jdbc:mysql://localhost:3306/test?";
conn=DriverManager.getConnection(url,user,password);
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return conn;
}
public Statement getStatement() throws SQLException{
return getConnection().createStatement();
}
public PreparedStatement getPrepareStatment(String sql) throws SQLException{
return getConnection().prepareStatement(sql);
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class JDBCDAO {
static Connection conn =null;
@Test
private static Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
try{
String user="root";
String password="";
String url="jdbc:mysql://localhost:3306/test?";
conn=DriverManager.getConnection(url,user,password);
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return conn;
}
public Statement getStatement() throws SQLException{
return getConnection().createStatement();
}
public PreparedStatement getPrepareStatment(String sql) throws SQLException{
return getConnection().prepareStatement(sql);
}
}