A little bit of Java database is not to configure the file, directly introduce a jar package on the line, which is different from PHP, but the specific database operation is relative to PHP has some complex, the main steps to do are as follows:
1: Introduction of JAR packages and SQL packages.
2: Load the database driver,
3: Set, connect, query, execute
4: Follow the steps to execute.
5: Retrieving results
The code is as follows:
PackageCom_jdbc_1;Importjava.sql.*;Importjavax.sql.*; Public class demon_1 { /** * @param args * * Public Static void Main(string[] args)throwsException {//define a connectionConnection ct=NULL;//Define query criteriaPreparedStatement ps=NULL;//define result setResultSet rs=NULL;//Select database driverClass.forName ("Com.mysql.jdbc.Driver");//Set connection informationCt=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test","Root","Toor");//Set queryPs=ct.preparestatement ("SELECT * from TestUser");//execute and get result setRs=ps.executequery ();intI=0;//Traversal output while(Rs.next ()) {System.out.println (Rs.getint (1)+" "+rs.getstring ("username")); } }}
How Java connects to a MySQL database