Home>
import java.sql. *;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
class conn {
 string url="jdbc:mysql://127.0.0.1/naridata";
 final string user="nari";
 final string pass="nariacc";
 connection conn=null;
 statement st=null;
 resultset rs=null;
 conn () {
  try {
   class.forname ("com.mysql.jdbc.driver");
  } catch (exception e) {
   e.printstacktrace ();
  }
  try {
   system.out.println ("in dbmanager");
   conn=drivermanager.getconnection (url, user, pass);
   string sql="create table test (id int, uid int)";
   st=conn.createstatement ();
   st.execute (sql);
  } catch (exception e) {
   e.printstacktrace ();
  }
 }
 void insert () {
  try {
   for (int i=0;i<10;i ++) {
    string sql="insert into test values ​​(" + i + "," + i + ")";
    st.execute (sql);
   }
  } catch (exception e) {
   e.printstacktrace ();
  }
 }
 void query () {
  try {
   string sql="select * from test";
   rs=st.executequery (sql);
   while (rs.next ()) {
    system.out.print ("res1:" + rs.getint (1) + "res2:" + rs.getint (2) + "\ n");
   }
  } catch (exception e) {
   e.printstacktrace ();
  }
 }
 void close () {
  try {
   rs.close ();
   st.close ();
   conn.close ();
  } catch (exception e) {
   e.printstacktrace ();
  }
 }
}
public class mysqltest {
 public static void main (string [] args) {
  //todo auto-generated method stub
  conn a1=new conn ();
  a1.insert ();
  a1.query ();
  a1.close ();
 }
}
jsp
  • Previous JS example of tab switching effect
  • Next Detailed object pointers and object arrays in C ++