Java connection to MySQL database via JDBC (operation)

Source: Internet
Author: User

Yesterday time limit just wrote how to connect the database, the purpose of connecting the database is to query, modify the data, just connected or meaningless.

The main method of Statement interface (java.sql.Statement) is used for data crying operation

int executeupdate (String sql) throws SQLException Update database
ResultSet executeQuery (String sql) throws SQLException query returns ResultSet result set

Code Demo
1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 ImportJava.sql.ResultSet;4 Importjava.sql.Statement;5 6  Public classMysqldemo {7 8      Public Static FinalString dbdriver = "Org.gjt.mm.mysql.Driver";//Drive Path9      Public Static FinalString Dburl = "Jdbc:mysql://localhost:3306/demo";//Database PathTen      Public Static FinalString USER = "root";//User name One      Public Static FinalString PASSWD = "changeme";//Password A      Public Static FinalString SQL = "INSERT into rj144 (Id,name,sex,tel) VALUES (123456, ' Zhangsan ', ' m ', ' 123456789 ')";//SQL statement to be executed -      Public Static voidMain (string[] args)throwsexception{ -         //TODO auto-generated Method Stub the         intCount//record the number of rows affected by the SQL statement -Connection con =NULL;//Database Connection Interface -Statement stmt =NULL;//Database Operation Interface -Class.forName (Dbdriver);//Load Driver +con = drivermanager.getconnection (DBURL,USER,PASSWD);//connecting to a database -stmt =con.createstatement ();  +Count = Stmt.executeupdate (SQL);//execute the SQL statement! and returns the number of rows affected to the variable count AStmt.close ();//Close Operation atCon.close ();//Close the database - System.out.println (count); -  -     } -  -}

After running output 1, it indicates that 1 rows of data were affected, i.e. the SQL statement was executed correctly.

Of course, just update the database is still not enough, next is the query query will be used to resultset interface yesterday said the ResultSet interface provides a method of processing the result set

Code Demo

Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement; Public classMysqldemo { Public Static FinalString dbdriver = "Org.gjt.mm.mysql.Driver";//Drive Path     Public Static FinalString Dburl = "Jdbc:mysql://localhost:3306/demo";//Database Path     Public Static FinalString USER = "root";//User name     Public Static FinalString PASSWD = "Changme";//Password     Public Static FinalString SQL = "SELECT * from rj144";//SQL statement to be executed     Public Static voidMain (string[] args)throwsexception{//TODO auto-generated Method StubConnection con =NULL;//Database Connection InterfaceResultSet rs =NULL; Statement stmt=NULL;//Database Operation InterfaceClass.forName (Dbdriver); //Load DriverCon= Drivermanager.getconnection (DBURL,USER,PASSWD);//connecting to a databasestmt =con.createstatement (); RS= Stmt.executequery (SQL);//executes the SQL statement, storing the result set in an instance of Resuultset                 while(Rs.next ()) {System.out.println ("Study Number:" + rs.getint ("id") + "\ t name" + rs.getstring ("name")                                 + "\ t gender" + rs.getstring ("sex") + "T phone" + rs.getstring ("tel"));//output data from resultset instance Rs} stmt.close (); //Close OperationCon.close ();//Close the database    }}

Run output

School Number: 123456 name zhangsan sex m phone 123456789

The above basic is the connection and operation of the database, but it may be the problem of encoding the Chinese data from the Java update to MySQL is not correct, as a freshman students are self-study of the limited energy has not yet to delve into this, if you do need to search by themselves

Java connection to MySQL database via JDBC (operation)

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.