package java_web;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class preStateDemo {
public final static String URL="jdbc:mysql://localhost:3306/test";
public final static String USERNAME="root";
public final static String PASSWORD="";
public final static String DRIVER="com.mysql.jdbc.Driver";
/**
*
* @param te
*
* 使用PreparedStatement插入数据
*/
public static testDemo findById(int id){
testDemo te=null;
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "select * from test where id=?";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
//设置占位符对应的值
ps.setInt(1,id);
ResultSet rs=ps.executeQuery();
if(rs.next()){
te=new testDemo();
te.setId(rs.getInt(1));
te.setName(rs.getString(2));
te.setSex(rs.getInt(3));
te.setVtime(rs.getString(4));
//java.sql.Date date=rs.getDate(4)
//java.util.Date date=rs.getDate(4)
//java.setDate(4,new java.sql.Date(date.getTime()))
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return te;
}
public static void delete(testDemo te){
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "delete from test where id=?";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
//设置占位符对应的值
ps.setInt(1,te.getId());
ps.executeUpdate();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void update(testDemo te){
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "update test set name=?,sex=?,vtime=? where id=?";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
//设置占位符对应的值
ps.setString(1,te.getName());
ps.setInt(2,te.getSex());
ps.setString(3,te.getVtime());
ps.setInt(4,te.getId());
ps.executeUpdate();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void insert(testDemo te){
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "insert into test(name,sex,vtime)values(?,?,?)";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
//设置占位符对应的值
ps.setString(1,te.getName());
ps.setInt(2,te.getSex());
ps.setString(3,te.getVtime());
ps.executeUpdate();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
//testDemo ter=new testDemo("fill",1,"12313123");
//insert(ter);
testDemo ter=new testDemo("fill_good",1,"12313123",7);
//update(ter);
//delete(ter);
ter=findById(6);
System.out.println(ter.getName());
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class preStateDemo {
public final static String URL="jdbc:mysql://localhost:3306/test";
public final static String USERNAME="root";
public final static String PASSWORD="";
public final static String DRIVER="com.mysql.jdbc.Driver";
/**
*
* @param te
*
* 使用PreparedStatement插入数据
*/
public static testDemo findById(int id){
testDemo te=null;
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "select * from test where id=?";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
//设置占位符对应的值
ps.setInt(1,id);
ResultSet rs=ps.executeQuery();
if(rs.next()){
te=new testDemo();
te.setId(rs.getInt(1));
te.setName(rs.getString(2));
te.setSex(rs.getInt(3));
te.setVtime(rs.getString(4));
//java.sql.Date date=rs.getDate(4)
//java.util.Date date=rs.getDate(4)
//java.setDate(4,new java.sql.Date(date.getTime()))
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return te;
}
public static void delete(testDemo te){
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "delete from test where id=?";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
//设置占位符对应的值
ps.setInt(1,te.getId());
ps.executeUpdate();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void update(testDemo te){
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "update test set name=?,sex=?,vtime=? where id=?";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
//设置占位符对应的值
ps.setString(1,te.getName());
ps.setInt(2,te.getSex());
ps.setString(3,te.getVtime());
ps.setInt(4,te.getId());
ps.executeUpdate();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void insert(testDemo te){
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "insert into test(name,sex,vtime)values(?,?,?)";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
//设置占位符对应的值
ps.setString(1,te.getName());
ps.setInt(2,te.getSex());
ps.setString(3,te.getVtime());
ps.executeUpdate();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
//testDemo ter=new testDemo("fill",1,"12313123");
//insert(ter);
testDemo ter=new testDemo("fill_good",1,"12313123",7);
//update(ter);
//delete(ter);
ter=findById(6);
System.out.println(ter.getName());
}
}