Java Learning Connection MySQL

Source: Internet
Author: User

The first step is to add Mysql-connector-java-5.1.10-bin.ja to the System Java project file

http://download.csdn.net/detail/u014112584/7359185

Mysql-----> Right click to select the Properties property--------------->add External JARS





Test examples


Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.SQLException; Import Com.mysql.jdbc.preparedstatement;import Com.mysql.jdbc.statement;public class Mysqltest {static String    Drivername= "Com.mysql.jdbc.Driver";    static string Url= "Jdbc:mysql://localhost:3306/expression";//point to Data Source static string username= "root";    Static String password= "";    Static Java.sql.Statement Stmt=null;    Static ResultSet Re=null;    Static Connection Conn=null;    Static PreparedStatement Pstm=null; /* * constructor is initialized */public mysqltest () {try{class.forname (drivername);//drive is loaded into the running environment, when loaded, the driver automatically to Drivermana    GER Completed Registration System.out.println ("Create driver Success");    }catch (ClassNotFoundException e) {e.printstacktrace ();   }/* * GET connection */public static Connection getconnection () {conn=null;   try{conn= (Connection) drivermanager.getconnection (URL, username, password);//with Driver and connection address, you need to use DriverManager to get the connection SYSTEM.OUT.PRINTLN ("Connection database succeeded! ");   }catch (SQLException e) {e.printstacktrace ();   } return conn; /** * Close Connection * @param args */public static void free (ResultSet rs,connection Conn,java.sql.statement St MT2) {if (rs!=null) {try {rs.close ()},} catch (SQLException e) {//TODO auto-generated catch BlockSystem.out.println (" Shutdown resultset failed! "); E.printstacktrace ();} Finally{if (conn!=null) {try {conn.close ()} catch (SQLException e) {//TODO auto-generated catch BlockSystem.out.println ("Shutdown Connection failed!") "); E.printstacktrace ();} Finally{if (stmt2!=null) {try {stmt2.close ()} catch (SQLException e) {//TODO auto-generated catch block System.out.print ln ("Shutdown statement failed!    "); E.printstacktrace ();}}}}}}    public static void Main (String[]args) {mysqltest.getconnection (); try {stmt=conn.createstatement ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} try {re=stmt.executequery ("SELECT * from Data"),} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacKtrace ();} int I=1;try {while (Re.next ()) {System.out.println (i++);}} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Free (RE,CONN,STMT);    System.out.println ("OK"); }}

More Java Connection databases

http://download.csdn.net/detail/u014112584/7359179


Related Article

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.