The examples in this article describe the methods of adding, deleting, updating, and finding JDBC operations databases. Share to everyone for your reference, specific as follows:
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 the method stub/
/create ();
Update ();
Delete ();
Read ();
The static void Delete () throws SQLException {Connection conn=null;
Statement St=null;
ResultSet Resultset=null;
try {//2. Establish a connection conn=jdbcutils.getconnection ();
Single Case design pattern conn=jdbcutilssingle.getinstance (). getconnection ();
3. Create statement st=conn.createstatement ();
4. Execution Statement 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. Establish a connection conn=jdbcutils.getconnection (); Single Case design pattern conn=jdbcutilssingle.getinstance (). GetConnection ();
3. Create statement st=conn.createstatement ();
4. Execute statement 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. Establish a connection conn=jdbcutils.getconnection ();
Single Case design pattern conn=jdbcutilssingle.getinstance (). getconnection ();
3. Create statement st=conn.createstatement ();
4. Execute statement 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. Establish a connection conn=jdbcutils.getconnection ();
Single Case design pattern conn=jdbcutilssingle.getinstance (). getconnection (); 3. CreateStatement st=conn.createstatement ();
4. Execution Statement Resultset=st.executequery ("Select Id,name,birthday,money from user");
5. Processing results 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. Releasing resources 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 ();
}
}
}
}
}
I hope this article will help you with your Java programming.