把数据库连接信息放到.properties配置文件中,方便以后修改
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;
import pojo.Users; //数据库表实体类
public class JdbcTest {
/**
* @param args
*/
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String strSql=null;
Users bean=null;
String name=null;
String password=null;
//构造方法
public JdbcTest(){
Properties properties = new Properties(); //实例化Properties类
try {
//读取src下的配置文件,我的配置文件在src下,注意路径,前面的/
properties.load(this.getClass().getResourceAsStream("/jdbc.properties"));
String driver = properties.getProperty("db.driver"); //获取配置文件中键值对的值
String url = properties.getProperty("db.url");
String user = properties.getProperty("db.user");
String password = properties.getProperty("db.password");
try {
Class.forName(driver); //注册驱动
//连接数据库,获得conn连接对象
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//在这里写了一个测试类,其实是构造方法的实现过程,都用于创建连接,并获得连接对象
public void JdbcTest1(){
String driver="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/jhwoa";
String user = "root";
String password="root";
try{
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
}catch(SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//插入
//在这里用的是statement对象,其实对于动态参数的sql操作应该用Prestatement对象
public String insert(String name,String password){
try {
strSql="INSERT INTO users(name,password) VALUES('"+name+"','"+password+"')";
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
System.out.println("插入语句执行成功:"+strSql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失败");
}
return "ok";
}
//更新
public String update(long id,String name,String password){
try{
strSql= "UPDATE users SET name='"+name+"',password='"+password+"'";
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
System.out.println("更新完成");
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("更新失败");
}
return "ok";
}
//删除
public String delete(long id){
try {
strSql="delete from users";
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
System.out.println("删除完成");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "ok";
}
//查询
public String select(long id){
try{
strSql="select * from users where id='"+id+"'";
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(strSql);
while(rs.next()){
System.out.println(rs.getString("name"));
System.out.println(rs.getString("password"));
}
}catch(SQLException e){
e.printStackTrace();
}
return "ok";
}
//关闭资源,注意关闭顺序,从里向外关
public void close(){
if(rs!=null){
try {
rs.close();
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs=null;
}
}
}