Java Database Connector Step:
(Eclipse)
You can import the jar package by right-clicking on the project item, tapping the properties below, and then following the sequence of keywords.
Key points: 1. Java Build Path
2.Libraries
3.ADD External JARs (add local JARs pack)
4.Apply
JDBC Common interfaces, classes:
1. Database-driven
Driver interface and DriverManager class
2. Database connection
Connection class
3. Execute the SQL text
Statement class
PreparedStatement class (can contain placeholders)
Code reference:
PackageCom.zczr.ja01;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.sql.Connection; Public classjdbc_test01 { Public Static voidMain (string[] args) {Connection con=NULL; Statement Stat=NULL; ResultSet RS=NULL ; //Jdbc-java Database Connector Try { //load the MySQL driver and load the MySQL driver into the Drivermanage classClass.forName ("Com.mysql.jdbc.Driver"); //Connection AddressString url = "Jdbc:mysql://127.0.0.1:3306/goods"; //User nameString userName = "root"; //User PasswordString password = "1234"; //The database link address, database user name, password to be passed by the driver administrator to obtain the connection object (Connection) of the databaseCon =drivermanager.getconnection (Url,username,password); //Writing SQL statementsString sql = "SELECT * FROM Goodsinfo"; //get the execution object for SQLStat =con.createstatement (); /*** Use SQL Execution object to execute an already written SQL statement * and return an execution result set (ResultSet)*/RS=stat.executequery (SQL); while(Rs.next ()) {intid = rs.getint ("GID"); String name= Rs.getstring ("Gname"); String type= Rs.getstring ("Gtype"); DoublePrice = Rs.getdouble ("Price"); intnum = rs.getint ("num"); intOrder = Rs.getint ("oder"); String SR= ID + "+ name +" "+" "+ Price +" "+ num +" "+ order +" \ n "; System.out.println ("Query result is: \ n" +SR); } } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ Try { if(rs! =NULL) {rs.close (); } if(Stat! =NULL) {rs.close (); } if(Con! =NULL) {rs.close (); } }Catch(SQLException e) {e.printstacktrace (); } } } }
MySQL---JDBC