JDBC操作數(shù)據(jù)庫的增加、刪除、更新、查找實例分析
更新時間:2015年10月19日 14:11:32 作者:煙大洋仔
這篇文章主要介紹了JDBC操作數(shù)據(jù)庫的增加、刪除、更新、查找方法,以完整實例形式分析了Java基于JDBC連接數(shù)據(jù)庫及進(jìn)行數(shù)據(jù)的增刪改查等技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了JDBC操作數(shù)據(jù)庫的增加、刪除、更新、查找方法。分享給大家供大家參考,具體如下:
package cn.com.JDBC;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CRUD {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
//create();
//update();
delete();
read();
}
static void delete() throws SQLException
{
Connection conn=null;
Statement st=null;
ResultSet resultset=null;
try {
//2.建立連接
conn=JdbcUtils.getConnection();
//單例設(shè)計模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.創(chuàng)建語句
st=conn.createStatement();
//4.執(zhí)行語句
String sql="delete from user where id>5";
int i=st.executeUpdate(sql);
System.out.println("i="+i);
} finally
{
JdbcUtils.free(resultset, st, conn);
}
}
static void update() throws SQLException
{
Connection conn=null;
Statement st=null;
ResultSet resultset=null;
try {
//2.建立連接
conn=JdbcUtils.getConnection();
//單例設(shè)計模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.創(chuàng)建語句
st=conn.createStatement();
//4.執(zhí)行語句
String sql="update user set money=money+20";
int i=st.executeUpdate(sql);
System.out.println("i="+i);
} finally
{
JdbcUtils.free(resultset, st, conn);
}
}
static void create() throws SQLException
{
Connection conn=null;
Statement st=null;
ResultSet resultset=null;
try {
//2.建立連接
conn=JdbcUtils.getConnection();
//單例設(shè)計模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.創(chuàng)建語句
st=conn.createStatement();
//4.執(zhí)行語句
String sql="insert into user(name,birthday,money) values('wy','2011-09-23','2894656')";
int i=st.executeUpdate(sql);
System.out.println("i="+i);
} finally
{
JdbcUtils.free(resultset, st, conn);
}
}
static void read() throws SQLException
{
Connection conn=null;
Statement st=null;
ResultSet resultset=null;
try {
//2.建立連接
conn=JdbcUtils.getConnection();
//單例設(shè)計模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.創(chuàng)建語句
st=conn.createStatement();
//4.執(zhí)行語句
resultset=st.executeQuery("select id,name,birthday,money from user");
//5.處理結(jié)果
while(resultset.next())
{
System.out.println(resultset.getObject("id"));
System.out.println(resultset.getObject("name"));
System.out.println(resultset.getObject("birthday"));
System.out.println(resultset.getObject("money"));
}
} finally
{
JdbcUtils.free(resultset, st, conn);
}
}
}
package cn.com.JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtils
{
private static String url="jdbc:mysql://localhost:3306/jdbc";
private static String user="root";
private static String password="123";
private JdbcUtils()
{
}
static
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection(url, user, password);
}
public static void free(ResultSet resultset,Statement st,Connection conn)
{
//6.釋放資源
try{
if(resultset!=null)
resultset.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
if(st!=null)
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
希望本文所述對大家Java程序設(shè)計有所幫助。
相關(guān)文章
Java虛擬機(jī)調(diào)用Java主類的main()方法
這篇文章主要介紹了Java虛擬機(jī)調(diào)用Java主類的main()方法,前一篇文章我們介紹了關(guān)于Java虛擬機(jī)HotSpot2021-11-11
linux環(huán)境下java程序打包成簡單的hello world輸出jar包示例
這篇文章主要介紹了linux環(huán)境下java程序打包成簡單的hello world輸出jar包,結(jié)合簡單hello world輸出程序示例分析了Linux環(huán)境下的java可執(zhí)行jar包文件的生成相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
詳解Spring Boot使用系統(tǒng)參數(shù)表提升系統(tǒng)的靈活性
Spring Boot項目中常有一些相對穩(wěn)定的參數(shù)設(shè)置項,其作用范圍是系統(tǒng)級的或模塊級的,這些參數(shù)稱為系統(tǒng)參數(shù)。這些變量以參數(shù)形式進(jìn)行配置,從而提高變動和擴(kuò)展的靈活性,保持代碼的穩(wěn)定性2021-06-06

