MyEclipse + Mysql (2), myeclipsemysql
The previous section describes how to connect to mysql in Myeclipse.
This section describes how to access data B in the mysql database in a java program and perform simple operations.
Create a javaProject and enter the following java code:
1 package link; 2 import java. SQL. *; 3 4/** 5 * connection to MySQL using JDBC 6 * DataBase: fuck, table: person; 7 * use myeclipse to add, delete, modify, and query mysql databases. 8 */9 10 public class JDBCTest {11 public static Connection getConnection () throws SQLException, 12 java. lang. classNotFoundException13 {14 // Step 1: load the MySQL JDBC driver 15 Class. forName ("com. mysql. jdbc. driver "); 16 17 // get the connection url, the user name and password that can access the MySQL database; jsj: Database Name 18 String url =" jdbc: mysql: // localhost: 3306/fuck "; 19 String username =" root "; 20 String password =" 123 "; 21 22 // Step 2: create an instance of the Connection class with the MySQL database 23 Connection con = DriverManager. getConnection (url, username, password); 24 return con; 25} 26 27 28 public static void main (String args []) {29 try30 {31 // Step 3: obtain the Connection class instance con and use con to create the Statement object class instance SQL _statement32 Connection con = getConnection (); 33 Statement SQL _statement = con. createStatement (); 34 35 // if a database with the same name exists, delete 36 // SQL _statement.executeUpdate ("drop table if exists student "); 37 // execute an SQL statement to generate a table named student 38 // SQL _statement.executeUpdate ("create table student (id int not null auto_increment, name varchar (20) not null default 'name', math int not null default 60, primary key (id ));"); 39 40 // insert data into the person table 41 SQL _statement.executeUpdate ("insert person values (3, 'liing', 98)"); 42 SQL _statement.executeUpdate ("insert person values (4, 'jiangshanc', 88) "); 43 SQL _statement.executeUpdate (" insert person values (5, 'wangjiawu ', 78) "); 44 SQL _statement.executeUpdate (" insert person values (6, 'duchangfeng', 100) "); 45 46 // Step 4: Execute the query and use the ResultSet class object to return the query result 47 String query =" select * from person "; 48 ResultSet result = SQL _statement.executeQuery (query); 49 50 // display the content of the person table in the data: 51 System. out. println ("data in the person table is as follows:"); 52 System. out. println ("------------------------"); 53 System. out. println ("no." + "" + "name" + "+" score "); 54 System. out. println ("------------------------"); 55 56 // process the obtained query results and operate 57 while (Result. next () 58 {59 int number = result. getInt ("number"); 60 String name = result. getString ("name"); 61 String mathsorce = result. getString ("mathsorce"); 62 63 // retrieves data from the database 64 System. out. println ("" + number + "" + name + "" + mathsorce); 65} 66 67 // close the connection and declare 68 SQL _statement.close (); 69 con. close (); 70 71} catch (java. lang. classNotFoundException e) {72 System. err. print ("ClassNotFoundException"); 73 System. err. println (e. getMessage (); 74} catch (SQLException ex) {75 System. err. println ("SQLException:" + ex. getMessage (); 76} 77} 78 79}
Bytes ----------------------------------------------------------------------------------------
Note that you need to modify the settings.
For example, the url and account, and password must be consistent with your own.
The data to be accessed must match the type in the database to print the correct result.
Bytes ------------------------------------------------------------------------------------------
Right-click the project name --> Build Path --> Configure bicould Path --> Libraries --> Add External JARs --> Add a jdbc package (For details, refer to Mysql's simple use (1 )) ---> OK
___________________________________________________________________________________________________________________________________________________________
In this case, a Referenced Libraries package file is added to the package, indicating that the configuration is successful.
Bytes ------------------------------------------------------------------------------------------
Click Run as ---> Run Java Application ---> JDBCTest -- link --->. The result is as follows: