Java Chapter 27th JDBC

Source: Internet
Author: User

JDBC templates

try {

Class.forName (JDBC driver Class);

1. Load the JDBC Driver

Class. forname ("Com.mysql.jdbc.Driver");

2. Establish a connection with the database

Connection con=drivermanager.getconnection (URL, database user name, password);

URLs are used to identify the database

PreparedStatement Pstmt=con. PreparedStatement ("Query SQL statement");

ResultSet Rs=pstmt.executequery ();

Executes the SQL statement and gets the result returned

while (Rs.next ()) {

int X=rs.getint ("a");

String s=rs.getstring ("B");

Float f=rs.getfloat ("C");

Processing the results of a drop back

}

Rs.close ();

Pstmt.close ();

Con.close (); Close connection

Freeing resources

Package com.test;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Java.util.Scanner;


public class Test1 {


/**
* @param args
*/
public static void Main (string[] args) {
Connection Conn=null;
PreparedStatement Pstmt=null;
ResultSet Rst=null;

try {
1. Load Driver
Class.forName ("Com.mysql.jdbc.Driver");
2. Establish the connection
Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/nyschool? Characterencoding=utf-8 "," root "," root ");
SYSTEM.OUT.PRINTLN ("Build connection succeeded!! ");
3. Create the Preparestatement interface and compile the SQL statement
Pstmt=conn.preparestatement ("SELECT * from student");
Perform the Delete
Scanner input=new Scanner (system.in);
System.out.print ("Please enter the student number to be deleted:");
int Stuno=input.nextint ();
Two methods:
Method 1:string sql= "DELETE from student WHERE stuno=" +stuno;
Method 2:string sql= "DELETE from student WHERE stuno=7";

Perform the Add
System.out.print ("Please enter the number of learners added:");
int Stuno=input.nextint ();
System.out.print ("Please enter the gender of the participant added:");
String Name=input.next ();
System.out.print ("Please enter the address added:");
String Phone=input.next ();
//
String sql= "INSERT into student VALUES (?, ' a2323 ', ' QQ ',?, 1,?, ' Jiangxi Nanchang ', sysdate (), ' qq.com ')";
Pstmt=conn.preparestatement (SQL); Pre-compilation method
Assign a value to a placeholder
Pstmt.setint (1, Stuno);
Pstmt.setstring (2, name);
Pstmt.setstring (3, phone);
System.out.print ("sql:" +sql);
4. Execute the SQL statement and get the return result
Rst=pstmt.executequery (); Execute Query
int num=pstmt.executeupdate (); Perform additions, deletions and changes
5. Processing return results
if (num>0) {
System.out.println ("\ n Add success! ");
}else{
System.out.println ("\ n Add failed! ");
}

while (Rs.next ()) {

int X=rs.getint ("a");

String s=rs.getstring ("B");

Float f=rs.getfloat ("C");

Processing the results of a drop back



} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();

}

Java Chapter 27th JDBC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.