Package Mysqltest;import Java.sql.drivermanager;import Java.sql.resultset;import com.mysql.jdbc.connection;import Com.mysql.jdbc.preparedstatement;import Com.mysql.jdbc.statement;public class MysqlTest02 {public static void main ( String[] args) {//connection urlstring URL = "JDBC:MYSQL://10.1.1.136:3306/JAVADB";//connection Driverstring Driver = " Com.mysql.jdbc.Driver ";//Username String name =" root ";//password string pwd =" root "; Try{class.forname (Driver); Connection Connection = (Connection) drivermanager.getconnection (URL, name, PWD); SYSTEM.OUT.PRINTLN ("Connection Success");//Add//string sql = "INSERT into TEST (Name,func) VALUES (?,?)"; /preparedstatement statement = (preparedstatement) connection.preparestatement (SQL);//statement.setobject (1, "高文斌") ;//statement.setobject (2, "ready to find a job");//int result = Statement.executeupdate ();//if (result = = 1) {//system.out.println (" Insert Success ");//query//string SQL1 =" SELECT * from TEST WHERE ID >? ";//}//connection.commit /preparedstatement sta1 = (preparedstatement) connection.preparestatEment (SQL1);//sta1.setint (1, 1);//resultset re = Sta1.executequery ();//while (Re.next ()) {//string names = re.getstring (2);//string func = re.getstring (3);//system.out.println (names+ "\ t:" +func);//}//system.out.println ("Query Complete");////Delete String DDL = "Delete from TEST where name = ' Gao Wenbin '";//statement sta = (Statement) connection.createstatement ();//int eff = Sta.executeupdate (DDL);//system.out.println (EFF);//change Statement s = (Statement) connection.createstatement (); int B = S.executeupdate ("Update TEST set name= ' Zhou Wen King ' where name= ' John Doe '"); System.out.println (b);} catch (Exception e) {System.out.println (e);}}}
Preparatory work:
1. Download Mysql-connector-java-5.1.45.zip package to Local
2. Configure the remote database configuration file, my copy is on the virtual machine, the file location in/etc/mysql/mysql.conf.d/mysqld.cnf, find bind-address = 127. 0.0. 1 commented out, allow remote access
JAVA operation remote MySQL database for single-table additions and deletions operation