Scrollable result set

Let the result set roll

//=================================================
// File Name : JDBC20_demo
//------------------------------------------------------------------------------
// Author : Common import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Scanner; // The main class
//Function : JDBC20_demo
public class JDBC20_demo { // Definition MySQL Database driver for
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
// Definition MySQL Database connection address
public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
//MySQL Database connection user name
public static final String DBUSER = "root";
//MySQL Database connection password
public static final String DBPASS = "123456"; public static void main(String[] args) throws Exception{
// TODO Automatically generated method stubs
Connection conn = null; // Database connection
PreparedStatement pstmt = null; // Database operation
ResultSet rs = null; // Save the result set String sql = "SELECT id,name,password,age,sex,birthday "+ " FROM user";
Class.forName(DBDRIVER); // Load driver
// Connect MySQL Database time , To write the user name and password of the connection
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
// Instantiation PreparedStatement
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = pstmt.executeQuery(); // Inquire about System.out.println(" The second data :");
rs.absolute(1); // Output No 2 strip , from 0 Start
print(rs,1); System.out.println(" The first data :");
rs.beforeFirst(); // Output No 1 strip
print(rs,1); System.out.println(" The second data :");
rs.afterLast(); // Output last 1 strip
print(rs,-1);
} public static void print(ResultSet rs,int re) throws Exception{
if(re>0){
rs.next(); // Output from front to back
}else{
rs.previous(); // There is a backward forward output
}
int id = rs.getInt(1);
String name = rs.getString(2);
String pass = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
java.util.Date d = rs.getDate(6);
System.out.println(" Number :"+ id);
System.out.println(" full name :"+name);
System.out.println(" password :"+pass);
System.out.println(" Age :"+age);
System.out.println(" Gender :"+sex);
System.out.println(" Birthday :"+d);
} }

Insert data using result sets

Directly in user Add data to the table

//=================================================
// File Name : JDBC20_demo
//------------------------------------------------------------------------------
// Author : Common import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Scanner; // The main class
//Function : JDBC20_demo
public class JDBC20_demo { // Definition MySQL Database driver for
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
// Definition MySQL Database connection address
public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
//MySQL Database connection user name
public static final String DBUSER = "root";
//MySQL Database connection password
public static final String DBPASS = "123456"; public static void main(String[] args) throws Exception{
// TODO Automatically generated method stubs Connection conn = null; // Database connection
PreparedStatement pstmt = null; // Database operation
ResultSet rs = null; // Save the result set String sql = "SELECT id,name,password,age,sex,birthday "+ " FROM user";
Class.forName(DBDRIVER); // Load driver
// Connect MySQL Database time , To write the user name and password of the connection
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
// Instantiation PreparedStatement
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery(); // Instantiation ResulitSet object
rs.moveToInsertRow(); // Move to rows of data that can be inserted
rs.updateString("name", " Zhang San ");
rs.updateString("password", "zhangsan");
rs.updateInt("age", 14);
rs.updateString("sex", " Woman ");
rs.updateDate("birthday", new java.sql.Date(new java.util.Date().getTime()));
rs.insertRow(); // insert data
rs.close();
pstmt.close();
conn.close(); } public static void print(ResultSet rs,int re) throws Exception{
if(re>0){
rs.next(); // Output from front to back
}else{
rs.previous(); // There is a backward forward output
}
int id = rs.getInt(1);
String name = rs.getString(2);
String pass = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
java.util.Date d = rs.getDate(6);
System.out.println(" Number :"+ id);
System.out.println(" full name :"+name);
System.out.println(" password :"+pass);
System.out.println(" Age :"+age);
System.out.println(" Gender :"+sex);
System.out.println(" Birthday :"+d);
} }

Use the result set to update the data

Use limited queries , Inquire about id The number is 3 User information for , And update the information

Only when it is found can it be updated , If not, you can't update

//=================================================
// File Name : JDBC20_demo
//------------------------------------------------------------------------------
// Author : Common import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Scanner; // The main class
//Function : JDBC20_demo
public class JDBC20_demo { // Definition MySQL Database driver for
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
// Definition MySQL Database connection address
public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
//MySQL Database connection user name
public static final String DBUSER = "root";
//MySQL Database connection password
public static final String DBPASS = "123456"; public static void main(String[] args) throws Exception{
// TODO Automatically generated method stubs Connection conn = null; // Database connection
PreparedStatement pstmt = null; // Database operation
ResultSet rs = null; // Save the result set String sql = "SELECT id,name,password,age,sex,birthday "+ " FROM user WHERE id=?";
Class.forName(DBDRIVER); // Load driver
// Connect MySQL Database time , To write the user name and password of the connection
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
// Instantiation PreparedStatement
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
pstmt.setInt(1, 2); // to update 3 User
rs = pstmt.executeQuery(); // Instantiation ResulitSet object
rs.last(); // Move to last line
rs.updateString("name", " Xiao Ming Wang ");
rs.updateString("password", "wangxiaoming");
rs.updateInt("age", 24);
rs.updateString("sex", " male ");
rs.updateDate("birthday", new java.sql.Date(new java.util.Date().getTime()));
rs.updateRow(); // Update data
rs.close(); // Close result set
pstmt.close();
conn.close();
} public static void print(ResultSet rs,int re) throws Exception{
if(re>0){
rs.next(); // Output from front to back
}else{
rs.previous(); // There is a backward forward output
}
int id = rs.getInt(1);
String name = rs.getString(2);
String pass = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
java.util.Date d = rs.getDate(6);
System.out.println(" Number :"+ id);
System.out.println(" full name :"+name);
System.out.println(" password :"+pass);
System.out.println(" Age :"+age);
System.out.println(" Gender :"+sex);
System.out.println(" Birthday :"+d);
} }

Use the result set to delete data

Use deleteRow() Method

//=================================================
// File Name : JDBC20_demo
//------------------------------------------------------------------------------
// Author : Common import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Scanner; // The main class
//Function : JDBC20_demo
public class JDBC20_demo { // Definition MySQL Database driver for
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
// Definition MySQL Database connection address
public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
//MySQL Database connection user name
public static final String DBUSER = "root";
//MySQL Database connection password
public static final String DBPASS = "123456"; public static void main(String[] args) throws Exception{
// TODO Automatically generated method stubs Connection conn = null; // Database connection
PreparedStatement pstmt = null; // Database operation
ResultSet rs = null; // Save the result set String sql = "SELECT id,name,password,age,sex,birthday "+ " FROM user WHERE id=?";
Class.forName(DBDRIVER); // Load driver
// Connect MySQL Database time , To write the user name and password of the connection
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
// Instantiation PreparedStatement
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
pstmt.setInt(1, 2); // to update 3 User
rs = pstmt.executeQuery(); // Instantiation ResulitSet object
rs.last(); // Move to last line
rs.deleteRow(); // Delete data
rs.close(); // Close result set
pstmt.close();
conn.close();
} public static void print(ResultSet rs,int re) throws Exception{
if(re>0){
rs.next(); // Output from front to back
}else{
rs.previous(); // There is a backward forward output
}
int id = rs.getInt(1);
String name = rs.getString(2);
String pass = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
java.util.Date d = rs.getDate(6);
System.out.println(" Number :"+ id);
System.out.println(" full name :"+name);
System.out.println(" password :"+pass);
System.out.println(" Age :"+age);
System.out.println(" Gender :"+sex);
System.out.println(" Birthday :"+d);
} }

The batch

//=================================================
// File Name : JDBC20_demo
//------------------------------------------------------------------------------
// Author : Common import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Scanner; // The main class
//Function : JDBC20_demo
public class JDBC20_demo { // Definition MySQL Database driver for
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
// Definition MySQL Database connection address
public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
//MySQL Database connection user name
public static final String DBUSER = "root";
//MySQL Database connection password
public static final String DBPASS = "123456"; public static void main(String[] args) throws Exception{
// TODO Automatically generated method stubs Connection conn = null; // Database connection
PreparedStatement pstmt = null; // Database operation String sql = "INSERT INTO user (name,password,age,sex,birthday) "+ " VALUES (?,?,?,?,?)";
Class.forName(DBDRIVER); // Load driver
// Connect MySQL Database time , To write the user name and password of the connection
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
// Instantiation PreparedStatement
pstmt = conn.prepareStatement(sql);
for(int i=0;i<10;i++){
pstmt.setString(1, " Zhang San —"+i);
pstmt.setString(2, "mima—"+i);
pstmt.setInt(3, 20+i);
pstmt.setString(4, " male ");
pstmt.setDate(5, new Date(new java.util.Date().getTime()));
pstmt.addBatch(); // Join the batch and wait for execution
}
int temp[] = pstmt.executeBatch(); // Batch execution
pstmt.close();
conn.close();
} public static void print(ResultSet rs,int re) throws Exception{
if(re>0){
rs.next(); // Output from front to back
}else{
rs.previous(); // There is a backward forward output
}
int id = rs.getInt(1);
String name = rs.getString(2);
String pass = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
java.util.Date d = rs.getDate(6);
System.out.println(" Number :"+ id);
System.out.println(" full name :"+name);
System.out.println(" password :"+pass);
System.out.println(" Age :"+age);
System.out.println(" Gender :"+sex);
System.out.println(" Birthday :"+d);
} }

Java database ——JDBC 2.0 More articles on operation

  1. Java Database connection --JDBC Basic knowledge of ( Operating the database : Additions and deletions )

    One .JDBC brief introduction JDBC Is the connection java The bridge between application and database . What is? JDBC? Java Language access to a database specification , It's a set API. JDBC (Java Database Connectivit ...

  2. java database JDBC operation MySQL Databases are commonly used API Department tables and staff tables Create table Add data Query data

    package com.swift.department; import java.sql.Connection; import java.sql.PreparedStatement; import ...

  3. Java Use Jdbc operation MySql database ( One )

    This example is Java operation MySql Basic approach . Before this example , To install MySql, And configure the account password , Create a logininfo database , Create... In the database userinfo Data sheet . And add sample data to the table . ...

  4. Java Database connection ——JDBC Basic knowledge of ( Operating the database : Additions and deletions )

    One .JDBC brief introduction JDBC Is the connection java The bridge between application and database . What is? JDBC? Java Language access to a database specification , It's a set API. JDBC (Java Database Connectivit ...

  5. Java Database operation (JDBC)

    JDBC Java Database connection (Java DataBase Connectivity,JDBC) Used in Java The database operation function is realized in the program , It provides execution SQL sentence . Access to various databases , And for a variety of different databases ...

  6. Java And JDBC Operating the database

    DBC JDBC It's a set of interfaces , Here's what it actually does jar You have to implement classes in the package , To implement methods in a class through generic objects . step : ###1. Import driver jar Contract to the project ###2. Write code to register driver , We need to let the program know which driver to use ...

  7. Java adopt JDBC Conduct MySQL Database operation

    from : http://blog.csdn.net/tobetheender/article/details/52772157 Java adopt JDBC Conduct MySQL Database operation original  2016 year 10 month 10 ...

  8. Java Of JDBC operation

    Java Of JDBC operation [TOC] 1.JDBC introduction 1.1. What is? JDBC JDBC In terms of physical structure, it means java Language access to a database set of interfaces , Essentially, java The protocol between the language root database .JDBC Provide a set of ...

  9. Java SE 6 New characteristics : Java DB and JDBC 4.0

    http://www.ibm.com/developerworks/cn/java/j-lo-jse65/index.html For a long time , Because of a large number of ( Even almost all ) Of Java Applications depend on databases , how ...

Random recommendation

  1. FFmpeg Transcoding and screen capture

    transcoding (flv Transcode as MP4,libx264 yes MP4 Coding format , -b 3000k It's the bit rate , Bit rate ) ffmpeg -i /home/ghr/mp4/mp4.flv -vcodec libx264 -b ...

  2. Windows 7 Unable to uninstall and install IE11 Solutions for

    1. Empty  C:\Windows\TEMP\   Everything in 2. Run the command line as Administrator , Enter the following code on the command line : FORFILES /P %WINDIR%\servicing\Packages ...

  3. PE File structure learning

    PE:Portable Executable File Format( Portable executors ).Windows Platform mainstream executable file format ..exe And .dll Files are PE Format .32 It's called PE32,64 It's called PE ...

  4. wordpress Forget password reset

    Always log in with your browser to record your password wordpress, Until one day the system was reloaded , And the browser records are not backed up , As a result, I can't remember the password I set at the beginning …… -_-||| Sui google Once again , Found a direct modification of the database reset secret ...

  5. yesterday mac After the update , There's something wrong with the Internet again ...

    As shown in the figure ...

  6. http-server Installation and operation

    vue After the project is packaged, a dist Catalog , We want to run it directly dist Catalog ... In addition to copying static files to the server nginx Under the table of contents , Then start nginx To start the project , Another way is to install http-server  Direct start ...

  7. Jmeter A single long connection sends multiple Sample

    Mark since :https://blog.csdn.net/lykangjia/article/details/16337505 1. Thread group , In our test plan , Each thread simulates a user , Perform user login . etc. ...

  8. Ingenious solution windows Next copy Command does not accept too long path problem

    Today, I met someone who wrote bat In the document xcopy success , But some files are missing , Check the log , I found a lot of hints : “the system can not find the path specified.“ But to specify ...

  9. UML Example of class structure diagram

  10. Cloud Foundry v2 Deployment and operation and maintenance

    I wrote one before Guide for Cloud Foundry New Teamer. But it seems to be out of date , That meeting is mainly aimed at CF v1 Research conducted , Now it's in full swing V2 Times . So update on Clou ...