Java uses jdbc to connect to a database instance

Source: Internet
Author: User

Connect to sqlserver Database

Package jdbc; import java. SQL. connection; import java. SQL. driver; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; public class JdbcTest1 {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubString className = "com. microsoft. sqlserver. jdbc. SQLServerDriver "; // load the jdbc driver, Class. forName () Load try {Cl Ass. forName (className); // 11111111111111} catch (ClassNotFoundException e) {e. printStackTrace ();} Driver driver = null; // register the jdbc Driver and register it in the DriverManager class. registerDriver (driver) try {Driver = new com. microsoft. sqlserver. jdbc. SQLServerDriver (); /// // DriverManager. registerDriver (driver ); /// // 222222222222} catch (SQLEx Ception e) {// TODO Auto-generated catch blocke. printStackTrace ();} // 3 create a database Connection object. DriverManager establishes a Connection object and knows which database to connect to, Connection username and password Connection con = null; String url = "jdbc: sqlserver: // localhost: 1433; DatabaseName = db "; // jdbc url. The identification methods for different databases are different. String user = "sa"; String password = "123456"; try {con = DriverManager. getConnection (url, user, password ); /// //} catch (Exception e) {e. printStackTrace ();} // PreparedStatementPreparedStatement pstmt = null; String SQL = "select * from book where price
 ? "; Try {pstmt = con. prepareStatement (SQL); //} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} try {pstmt. setFloat (1, (float) 50.0); pstmt. setInt (2, 5);} catch (SQLException e) {e. printStackTrace ();} // execute the database SQL operation ResultSet rs = null using the statement object; try {rs = pstmt.exe cuteQuery (); ////////////////////////////////////////} catch (SQLException e) {// TODO Au To-generated catch blocke. printStackTrace ();} // process the database query result try {while (rs. next () {String isbn = rs. getString (1); String author = rs. getString ("author"); Float price = rs. getFloat ("price"); Integer number = rs. getInt ("number"); System. out. println ("isbn:" + isbn + "\ nauthor:" + author + "\ ntotal:" + price * number + "\ n");} catch (SQLException e) {e. printStackTrace ();} // close the resource. The order is the opposite: try {if (rs! = Null) {rs. close () ;}if (pstmt! = Null) {pstmt. close ();} if (con! = Null) {con. close () ;}} catch (SQLException e) {e. printStackTrace ();}}}

Connect to the mysql database

Package jdbc; import java. SQL. connection; import java. SQL. driver; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; public class JdbcTest {/** query the content in the database table, and display * @ param args */public static void main (String [] args) in the console output) {String className = "com. mysql. jdbc. driver "; // load the jdbc Driver, Class. forName () Load try {Class. forName (ClassName); // 11111111111111} catch (ClassNotFoundException e) {e. printStackTrace ();} Driver driver = null; // register the jdbc Driver and register it in the DriverManager class. registerDriver (driver) try {Driver = new com. mysql. jdbc. driver (); /// // DriverManager. registerDriver (driver ); /// // 222222222222} catch (SQLException e) // TODO Auto-generated c Atch blocke. printStackTrace ();} // 3 create a database Connection object. DriverManager establishes a Connection object and knows which database to connect to, Connection username and password Connection con = null; String url = "jdbc: mysql: // localhost: 3306/db "; // jdbc url. The identification methods for different databases are different. String user = "root"; String password = "123456"; try {con = DriverManager. getConnection (url, user, password ); /// //} catch (Exception e) {e. printStackTrace ();} // 4... prepare the work before operating the database. You can use Statement or PreparedStatement to operate the database. You can use the Connection object to obtain the Statement object // Statement stmt = null; try {stmt = con. createStatement (); //} catch (SQLException e) {e. printStackTrace ();}/** PreparedStatementPreparedStatement pstmt = null; String SQL = "select * from book where price
 ? "; Try {pstmt = con. prepareStatement (SQL); //} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} try {pstmt. setFloat (1, (float) 50.0); pstmt. setInt (2, 5);} catch (SQLException e) {e. printStackTrace ();} ** // execute the database SQL operation String SQL = "select * from book where price <50 and number> 5" by using the statement object "; resultSet rs = null; try {rs = stmt.exe cuteQuery (SQL );//////// //// //} Catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // process the database query result try {while (rs. next () {String isbn = rs. getString (1); String author = rs. getString ("author"); Float price = rs. getFloat ("price"); Integer number = rs. getInt ("number"); System. out. println ("isbn:" + isbn + "\ nauthor:" + author + "\ ntotal:" + price * number + "\ n");} catch (SQLException e) {e. pr IntStackTrace () ;}/// close the resource. The order is the opposite: try {if (rs! = Null) {rs. close () ;}if (stmt! = Null) {stmt. close () ;}if (con! = Null) {con. close () ;}} catch (SQLException e) {e. printStackTrace ();}}}


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.