Import java.sql.*;
Import Java.text.DateFormat;
Database query public class Selecttable {String dbdriver= "com.mysql.jdbc.Driver";
String dburl= "Jdbc:mysql://localhost:3306/sss"; Change string username= "root" according to the actual situation;
String password= "123";
Public Connection Getconn () {Connection conn=null;
try {class.forname (dbdriver);
catch (ClassNotFoundException e) {e.printstacktrace ();
try {conn = drivermanager.getconnection (Dburl,username,password);//Note three parameters}
catch (SQLException e) {e.printstacktrace ();
Return conn;
public void Select () {Connection conn = Getconn (); try{Statement stmt = conn.createstatement ();//Create Statement Object System.out.println ("Successfully connected to the database!")
"); String sql = "SELECT * from JDBC"; SQL ResultSet rs = stmt.executequery (sql) to execute;//Create a data object System.out.priNTLN ("id" + "T" + "name" + "T" + "brithday");
while (Rs.next ()) {System.out.print (Rs.getint (1) + "T");
System.out.print (rs.getstring (2) + "T");
System.out.print (Rs.getdate (3) + "T");
System.out.println ();
}}catch (Exception e) {e.printstacktrace ();}
public void Insert () {Connection conn = Getconn (); try{Statement stmt = Conn.createstatement (); System.out.println ("Successfully connected to the database!")
");
String sql = INSERT into JDBC (id,name,birthday) values (?,?,?);
PreparedStatement PST =conn.preparestatement (SQL);
DateFormat df = dateformat.getdateinstance ();
Java.util.Date dd = df.parse ("2000-12-12");//convert time of YYYY-MM-DD format to date long T = Dd.gettime ();
Java.sql.Date Date = new Java.sql.Date (t);
Pst.setint (1, 5);
Pst.setstring (2, "Limazhi");
Pst.setdate (3, date);
Pst.executeupdate ();
Select ();
}catch (Exception e) {e.printstacktrace ();} public static void Main (String args[]) {selecttable st = new Selecttable (); st.insERT (); }
}