代码:
1.直接调用插入:
DButil.java
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Connection;
public class DButil {
/*
* 打开数据库
*/
private static String driver;//连接数据库的驱动
private static String url;
private static String username;
private static String password;
static {
driver="com.mysql.jdbc.Driver";//需要的数据库驱动
url="jdbc:mysql://localhost:3306/test";//数据库名路径
username="root";
password="root";
}
public static Connection open()
{
try {
Class.forName(driver);
return (Connection) DriverManager.getConnection(url,username, password);
} catch (Exception e) {
System.out.println("数据库连接失败!");
// TODO Auto-generated catch block
e.printStackTrace();
}//加载驱动
return null;
}
/*
* 关闭数据库
*/
public static void close(Connection conn)
{
if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
jdbcconnection.java
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class jdbcconnection {
public static void main(String[] args) {
// TODO Auto-generated method stub
insert("yangxu","yangxu@qq.com");
}
static void insert(String name,String email)
{
String sql="insert into Haige(name,email) value(?,?)";
Connection conn=DButil.open();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setString(2,email);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
DButil.close(conn);
}
}
}
2.面向对象的方式
Customer.java
public class Customer {
int id;
String name;
String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
jdbcconnection.java(插入数据)
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class jdbcconnection {
public static void main(String[] args) {
// TODO Auto-generated method stub
//insert("yangxu","yangxu@qq.com");
Customer c=new Customer();
c.setName("zhangbing");
c.setEmail("zhangbing@qq.com");
insert(c);
}
static void insert(Customer c)
{
String sql="insert into Haige(name,email) value(?,?)";
Connection conn=DButil.open();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1,c.getName());
pstmt.setString(2,c.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
DButil.close(conn);
}
}
}
jdbcconnection.java(修改数据)
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class jdbcconnection {
public static void main(String[] args) {
// TODO Auto-generated method stub
//insert("yangxu","yangxu@qq.com");
Customer c=new Customer();
// c.setName("zhangbing");
// c.setEmail("zhangbing@qq.com");
//insert(c);
c.setId(1001);
c.setName("kaixin");
Update(c);
}
static void Update(Customer c)
{
String sql="update haige set name=? where id=?";
Connection conn=DButil.open();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1,c.getName());
pstmt.setInt(2,c.getId());;
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
DButil.close(conn);
}
}
}
jdbcconnection.java(删除数据)
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class jdbcconnection {
public static void main(String[] args) {
// TODO Auto-generated method stub
//insert("yangxu","yangxu@qq.com");
// Customer c=new Customer();
c.setName("zhangbing");
c.setEmail("zhangbing@qq.com");
// //insert(c);
// c.setId(1001);
// c.setName("kaixin");
// Update(c);
delete(1006);
}
static void delete(int id)
{
String sql="delete from haige where id=?";
Connection conn=DButil.open();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setInt(1,id);;
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
DButil.close(conn);
}
}
}
jdbcconnection.java(查询数据)
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class jdbcconnection {
public static void main(String[] args) {
// TODO Auto-generated method stub
//insert("yangxu","yangxu@qq.com");
// Customer c=new Customer();
c.setName("zhangbing");
c.setEmail("zhangbing@qq.com");
// //insert(c);
// c.setId(1001);
// c.setName("kaixin");
// Update(c);
//delete(1006);
Customer c=query(1005);
System.out.println(c.getId()+","+c.getName()+","+c.getEmail());
}
static Customer query(int id)
{
String sql="select * from haige where id=?";
Connection conn=DButil.open();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setInt(1,id);
ResultSet rs=pstmt.executeQuery();
if(rs.next())
{
String name=rs.getString(2);
String email=rs.getString(3);
Customer c=new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
return c;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
DButil.close(conn);
}
return null;
}
}
知识点总结:
PreparedStatement 只能静态操作SQL语句,PreparedStatement 通过使用静态符“?”,来预生成SQL语句,从而达到动态操作的功能