JDBC Six section-1

Source: Internet
Author: User
Tags stmt variable scope

Create a db.properties file to configure access to the database dbname=mysql/oracle//by reading the file to achieve different access to different databases import Java.io.*;import java.util.*; public class Jdbctestprogrammer1{public static void Main (string[] args) throws exception{// Read the configuration file via FileReader filereader reader = new FileReader ("db.properties");//Create a Property object Properties Prop = new properties ();// Read reader to memory via the Property object's Load method to generate a map collection prop.load (reader);//Close stream reader.close ();//GetProperty via Property object (string key) string dbname = Prop.getproperty ("dbname");//created by the Java reflection Mechanism Class C = Class.forName (dbname); Object obj = C.newinstance (); JDBC jdbc = (JDBC) obj;jdbc.getconnection ();}}

JDBC Programming Six steps

1: Registered Driver

2: Get a database connection

3: Execute database Operation object

4: Execute SQL statement

5: Working with query result sets

6: Close Resource


The first step: Implement the registration drive

1.1) Get the Drive object

1.2) Registration Drive

Import Java.sql.driver;import Java.sql.drivermanager;public class Drivertest {public static void main (string[] args) { try{//gets the Drive object driver Driver = new Com.mysql.jdbc.Driver ();//Register drive Drivermanager.registerdriver (driver);} catch (Exception e) {e.printstacktrace ();}}}

Step two: Get the connection to the database

Import Java.sql.connection;import Java.sql.driver;import Java.sql.drivermanager;public class JDBCTest {public static void Main (string[] args) {try{//1.1 Gets the drive object driver Driver = new Com.mysql.jdbc.Driver ();// 1.2 Registered driver Drivermanager.registerdriver (driver);//2 Gets the database connection string url = "Jdbc:mysql://127.0.0.1:3306/test"; String user = "root"; String password = "123456"; Connection conn = drivermanager.getconnection (URL, user, password); SYSTEM.OUT.PRINTLN (conn);} catch (Exception e) {e.printstacktrace ();}}}

Step three: Get the Operation object of the database

Import Java.sql.driver;import java.sql.drivermanager;import Java.sql.statement;import java.sql.Connection;public Class Jdbctest{public static void Main (string[] args) {try{//registered drive Driver Driver = new Com.mysql.jdbc.Driver ();D Rivermanag Er.registerdriver (driver);//Get database connection string url = "Jdbc:mysql://127.0.0.1:3306/test"; String user = "root"; String password = "123456"; Connection conn = drivermanager.getconnection (Url,user,password);//3: Gets the database operation object Statement stmt = Conn.createstatement ( ); System.out.println (stmt);} catch (Exception e) {e.printstacktrace ();}}}

Fourth step: Execute the SQL statement

import java.sql.connection;import java.sql.driver;import java.sql.drivermanager;import  java.sql.resultset;import java.sql.sqlexception;import java.sql.statement;public class  Jdbctest{public static void main (String[] args) {TRY {//1 registered driver driver driver =  new com.mysql.jdbc.driver ();D rivermanager.deregisterdriver (Driver);//2 Get database connection string url =   "JDBC:MYSQL://127.0.0.1:3306/JDBC"; string user =  "Root"; string password =  "123456"; Connection conn = drivermanager.getconnection (Url, user, password); SYSTEM.OUT.PRINTLN (conn);//3 Gets the database Operation Object Statement stat = conn.createstatement ();//4 Execute SQL statement: DQL statement- > Query string sql =  "select c_id, c_name , c_zip  from  Customers "; Resultset rs = stat.executequery (SQL); System.out.println (RS);} catch  (sqlexception e)  {e.printstacktraCE ();}}} 

Fifth step: Working with query result sets

import java.sql.connection;import java.sql.driver;import java.sql.drivermanager;import  java.sql.resultset;import java.sql.sqlexception;import java.sql.statement;public class  Jdbctest{public static void main (String[] args) {TRY {//1 registered driver driver driver =  new com.mysql.jdbc.driver ();D rivermanager.deregisterdriver (Driver);//2 Get database connection string url =   "JDBC:MYSQL://127.0.0.1:3306/JDBC"; string user =  "Root"; string password =  "123456"; Connection conn = drivermanager.getconnection (Url, user, password); SYSTEM.OUT.PRINTLN (conn);//3 Gets the database Operation Object Statement stat = conn.createstatement ();//4 Execute SQL statement: DQL statement- > Query string sql =  "select c_id, c_name , c_zip  from  Customers; "; Resultset rs = stat.executequery (SQL); System.out.println (RS);//5 processing Query results while (Rs.next ()) {//fetching data the first way string   Name= rs.getstring ("C_name"); Int id = rs.getint ("c_id");d ouble zip =  Rs.getdouble ("C_zip"); System.out.println (name+ "\t " +id+ "\t " +zip);/* Do not suggest the following wording, program readability is not strong string  name=  Rs.getstring (2); Int id = rs.getint (1);d ouble zip = rs.getdouble (3); System.out.println (name+ "\t " +id+ "\t " +zip);*/}}catch  (sqlexception e)  { E.printstacktrace ();}}}

Sixth step: Close Resources

Variables defined in the try cannot be read in finally because of the variable scope relationship, so local variables are required as global variables

import java.sql.connection;import java.sql.driver;import java.sql.drivermanager;import  java.sql.resultset;import java.sql.sqlexception;import java.sql.statement;public class  Jdbctest{public static void main (String[] args) {connection conn = null; statement stat = null; RESULTSET RS = NULL;TRY {//1 Registered Driver driver driver = new  Com.mysql.jdbc.Driver ();D rivermanager.deregisterdriver (Driver);//2 Get database connection string url =  "JDBC: Mysql://127.0.0.1:3306/jdbc "; string user =  "Root"; string password =  "123456"; Conn = drivermanager.getconnection (url, user,  password); SYSTEM.OUT.PRINTLN (conn);//3 Gets the database Operation Object Stat = conn.createstatement ();//4 Execute SQL statement: DQL statement, query string  sql =  "select c_id, c_name , c_zip  from customers;"; Rs = stat.executequery (SQL); System.out.prIntln (RS);//5 processing Query results while (Rs.next ()) {//fetching data in the first way string  name= rs.getstring ("C_name"); int id  = rs.getint ("c_id");d ouble zip = rs.getdouble ("C_zip"); System.out.println (name+ "\t " +id+ "\t " +zip);/* Do not suggest the following wording, program readability is not strong string  name=  Rs.getstring (2); Int id = rs.getint (1);d ouble zip = rs.getdouble (3); System.out.println (name+ "\t " +id+ "\t " +zip);*/}}catch  (sqlexception e)  { E.printstacktrace ();} FINALLY{//6 Close Resources    flashbacks off if (rs != null) {try {rs.close ();}  catch  (sqlexception e)  {e.printstacktrace ();}} if (stat != null) {try {stat.close ();}  catch  (sqlexception e)  {e.printstacktrace ();}} if (conn != null) {try {conn.close ();}  catch  (sqlexception e)  {e.printstacktrace ();}}}}

Attached: Executing DML statements     additions and deletions

import java.sql.connection;import java.sql.driver;import java.sql.drivermanager;import  java.sql.resultset;import java.sql.sqlexception;import java.sql.statement;public class  Jdbctest{public static void main (String[] args) {connection conn = null; statement  stat = null;int count = 0;try {driver driver =  new com.mysql.jdbc.driver ();D rivermanager.deregisterdriver (Driver); string url =  "JDBC:MYSQL://127.0.0.1:3306/JDBC"; string user =  "Root"; string password =  "123456"; Conn = drivermanager.getconnection (url, user,  password); stat = conn.createstatement ();//Execute SQL statement   :DML statement->insert update  delectstring sql_ins =  "Insert into t_user (name)  values  (' AAA ')"; count  = stat.executeupdate (Sql_ins); string sql_update =  "update t_user set name =  ' lll '   where id = 1 "; count =  stat.executeupdate (sql_update); string sql_del =  "delete from t_user where name =  ' aaa '"; count  = stat.executeupdate (Sql_del); System.out.println (count);}  catch  (sqlexception e)  {e.printstacktrace ();}}


This article is from the "Man On Dream Road" blog, please make sure to keep this source http://meyangyang.blog.51cto.com/12906086/1971378

JDBC Six section-1

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.