package jdbc_demo3; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Before; import org.junit.Test; import web09.jdbc_text1; import web09.jdbc_text3; import web09.jdbc_text4; /** * 测试工具类 * @author Administrator * */ public class jdbc_text2 { /** * */@Test public void textdeletByid() {Connection conn=null; PreparedStatement pstmt=null; try{ //1获取链接 conn=jdbc_text4.getconnection(); //2编写sql语句 String sql="delete from tbl_user where uid=?"; //3获取sql的语句对象 pstmt = conn.prepareStatement(sql); //4设置参数 pstmt.setInt(1, 3); //5执行插入炒作 int row =pstmt.executeUpdate(); if(row>0){ System.out.println("删除成功"); }else{ System.out.println("删除失败"); } }catch (Exception e){ throw new RuntimeException(e); }finally{ //6释放资源 jdbc_text3.release(conn, pstmt, null);; } } @Test //增加 public void testAdd() {Connection conn=null; PreparedStatement pstmt=null; try{ //1获取链接 conn=jdbc_text3.getconnection(); //2编写sql语句 String sql="insert into tbl_user values(null,?,?)"; //3获取sql的语句对象 pstmt = conn.prepareStatement(sql); //4设置参数 pstmt.setString(1, "lisi"); pstmt.setString(2, "hehe"); //5执行插入炒作 int row =pstmt.executeUpdate(); if(row>0){ System.out.println("插入成功"); }else{ System.out.println("插入失败"); } }catch (Exception e){ throw new RuntimeException(e); }finally{ //6释放资源 jdbc_text3.release(conn, pstmt, null);; } } @Test //查询 public void textfinduserbyid(){ Connection conn=null; PreparedStatement pstmt=null; ResultSet rs=null; try { //1.获取链接 conn = jdbc_text1.getconnection(); //2编写sql语句 String sql ="select * from tbl_user where uid=?"; //3获取执行sql语句对象 pstmt = conn.prepareStatement(sql); //4设置参数 pstmt.setInt(1, 2); //setint(第一个表示第一个获取位置,第二个表示值) //5设置查询操作 rs =pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getString(2)+"------------"+rs.getString("upassword")); } } catch (SQLException e) { e.printStackTrace(); }finally { jdbc_text1.release(conn, pstmt, rs); } } }
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;
import java.util.ResourceBundle;
public class jdbc_text4 {
private static String driver;
private static String url;
private static String username;
private static String password;
/*
* 静态代码块获取配置文件信息
*/
static{
try {
//1.通过当前类获取加载器
ClassLoader calssloader =jdbc_text3.class.getClassLoader();
//2.通过类加载器方法,获取一个输入流
InputStream is = calssloader.getResourceAsStream("db.properties");
//3.创建一个properties对象
Properties props =new Properties();
//4.获取输入流
props.load(is);
//5获取相关操作的值
driver=props.getProperty("driver");
url=props.getProperty("url");
username=props.getProperty("username");
password=props.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getconnection(){
Connection conn=null;
//賦值是爲了防止空指針
try {
Class.forName(driver);
//注册驱动
conn = DriverManager.getConnection(url,username,password);
//获取连接
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
通过流建立jdbc