manipulating databases through JDBC
PackageHead18;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importcom.mysql.jdbc.PreparedStatement; Public classJDBC { Public Static voidMain (string[] args) {//Load Driver Try{class.forname ("Com.mysql.jdbc.Driver"); System.out.println ("Load driver succeeded"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } //Database ConnectionString url= "jdbc:mysql://127.0.0.1:13306/lianxi1?user=root&password=123456" + "&useunicode=true& ; Characterencoding=utf-8 "; Connection Con=NULL; Try{con=drivermanager.getconnection (URL); System.out.println ("Database established successfully"); } Catch(SQLException e) {e.printstacktrace (); System.out.println ("Database setup Failed"); } //Create a handleStatement stmt=NULL; Try{stmt=con.createstatement (); System.out.println ("Build handle succeeded"); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } //action handle, insert statementString sql= "INSERT into BP1 VALUES (' 111 ', ' pie ', ' male ', ' 19930101 ', ' 95033 ')"; Try { BooleanBsql=stmt.execute (SQL); if(bSQL) {System.out.println ("Insert Failed"); }Else{System.out.println ("Insert Succeeded"); } } Catch(SQLException e) {e.printstacktrace (); } //operation handle,Query StatementsString sql1= "SELECT * from BP1"; Try{ResultSet rs=stmt.executequery (SQL1); intCol=rs.getmetadata (). getColumnCount (); while(Rs.next ()) { for(inti = 1; I <= col; i++) {System.out.print (rs.getstring (i)+ "\ T"); } System.out.println (""); } } Catch(SQLException e) {e.printstacktrace (); } }}
JDBC Connection database (Java db Connectivity,java)