Java to operate on the local database mysql and javamysql

Source: Internet
Author: User

Java to operate on the local database mysql and javamysql

Single-threaded version/*****/import java. SQL. *; import java. util. date; import org. omg. CORBA. PUBLIC_MEMBER;/*** @ author Administrator **/public class Mysql {public void Chaxun () {try {Class. forName ("com. mysql. jdbc. driver ");} catch (Exception e) {// TODO: handle exception e. printStackTrace ();} String url = "jdbc: mysql: // localhost: 3306/beijing"; // Connection connection of jabc; try {connection = DriverManager. getCon Nection (url, "root", "liwanlei"); Statement statement = connection. createStatement (); String SQL = "select * from student where id = 1"; ResultSet resultSet=statement.exe cuteQuery (SQL); if (resultSet. next () {System. out. println (resultSet. getString ("name");} statement. close (); connection. close ();} catch (Exception e) {// TODO: handle exception System. out. println (e) ;}} public void Change () {try {Class. forName ("com. mysql. jdbc. driver ");} catch (Exception e) {// TODO: handle exception e. printStackTrace (); System. out. println ("exception");} String url = "jdbc: mysql: // localhost: 3306/beijing"; // The jabc path Connection; try {connection = DriverManager. getConnection (url, "root", "liwanlei"); Statement statement = connection. createStatement (); String SQL = "UPDATE student SET name =? WHERE id =? "; PreparedStatement preparedStatement = connection. prepareStatement (SQL); preparedStatement. setString (1, "liwanlei"); preparedStatement. setInt (2, 1); preparedStatement.exe cuteUpdate (); System. out. println ("OK");} catch (Exception e) {// TODO: handle exception System. out. println ("exception") ;}} public void Adddate () {Date aDate = new Date (); try {Class. forName ("com. mysql. jdbc. driver ");} catch (Exception E) {// TODO: handle exception System. out. println ("failed to load the driver");} String url = "jdbc: mysql: // localhost: 3306/beijing"; Connection connection; try {connection = DriverManager. getConnection (url, "root", "liwanlei"); Statement statement = connection. createStatement (); String SQL = "insert into student (id, name, age, address) values (?,?,?,?) "; PreparedStatement preparedStatement = connection. prepareStatement (SQL); for (int I = 1; I <10; I ++) {preparedStatement. setInt (1, I); preparedStatement. setString (2, "liwanddlei" + I); preparedStatement. setInt (3, I * 333); preparedStatement. setString (4, "beijiddng" + I); preparedStatement.exe cuteUpdate ();} preparedStatement. close (); connection. close (); Date date = new Date (); System. out. println (date. getTime ()-aDate. getTime (); System. out. println ("successfully inserted");} catch (Exception e) {// TODO: handle exception} public static void main (String [] args) {// TODO Auto-generated method stub Mysql mysql = new Mysql (); mysql. adddate ();}}
Multi-threaded insert version import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. statement; import java. util. date;/***** // **** @ author lileilei * Threadmyql. java * December 9, 2017 */public class Threadmyql extends Thread {@ Override public void run () {Date aDate = new Date (); try {Class. forName ("com. mysql. jdbc. driver ");} catch (Exception e) {// TODO: handle exception cause e M. out. println ("failed to load the driver");} String url = "jdbc: mysql: // localhost: 3306/beijing"; Connection connection; try {connection = DriverManager. getConnection (url, "root", "liwanlei"); Statement statement = connection. createStatement (); String SQL = "insert into student (id, name, age, address) values (?,?,?,?) "; PreparedStatement preparedStatement = connection. prepareStatement (SQL); for (int I = 1; I <100000; I ++) {preparedStatement. setInt (1, I); preparedStatement. setString (2, "asdasdasd" + I); preparedStatement. setInt (3, I * 333); preparedStatement. setString (4, "asdasdasdasdasd" + I); preparedStatement.exe cuteUpdate ();} preparedStatement. close (); connection. close (); Date date = new Date (); System. out. println (date. getTime ()-aDate. getTime ()/1000); System. out. println ("successfully inserted");} catch (Exception e) {// TODO: handle exception} public static void main (String [] args) {for (int I = 1; I <= 10; I ++) {new Threadmyql (). start ();}}}

There is little data relevance and no need to do performance. After a while, no need is found.

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.