First create a database test1, and then create a table under database test1 user
Open command Line input mysql-h 127.0.0.1-u root-p----> re-enter the password login MySQL
Enter CREATE DATABASE test1;
Input use test1;
Input
CREATE TABLE User (a varchar (a), B varchar (), C varchar (20));
Insert a few more sets of data
INSERT into User (a,b,c) VALUES (' Lisi ', ' 3001 ', ' 100 ');
INSERT into User (a,b,c) VALUES (' Lihua ', ' 3002 ', ' 150 ');
INSERT into User (a,b,c) VALUES (' Zhangsan ', ' 3003 ', ' 200 ');
A simple table establishes the
--------------------------------------------------------------------------------------------------------------- ------------------------
Here is the Java program:
Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.SQLException; Import Java.sql.statement;public class Test1 {public static void main (string[] args) throws Exception { test ();} static void Test () throws sqlexception{ //1 registered driver drivermanager.registerdriver (new Com.mysql.jdbc.Driver ()); String url= "Jdbc:mysql://localhost:3306/test1"; String user = "root"; String password= ""; Establish connection Connection conn = drivermanager.getconnection (Url,user,password); Create statement Statement St =conn.createstatement (); Execute statement ResultSet RS =st.executequery ("SELECT * from user"); while (Rs.next ()) { System.out.println ((String) rs.getobject (1) +rs.getobject (2) +rs.getobject (3)); } Close connection rs.close (); St.close (); Conn.close ();}}
After running the result is
lisi3001100
lihua3002150
zhangsan3003200
The first simple JDBC program