java - mySQL

1572 단어

public tt() {
 // TODO Auto-generated constructor stub


 try {
 Class.forName("com.mysql.jdbc.Driver");
 
 Connection conn = (Connection) DriverManager.getConnection(Driver, username,password);


 java.sql.Statement stmt = conn.createStatement();


 
 ResultSet rs=stmt.executeQuery("select * from aa1");
 
 int a=0;
 while(rs.next()){
 System.out.println(String.valueOf(a)+rs.getString("val"));
 a++;
 }
 
 
 
 
 } catch (Exception e) {
 e.printStackTrace();


 }


 }
}


// 1.     
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }// Mysql    
  
  // 2.         
  java.sql.Connection conn = java.sql.DriverManager.getConnection(
    "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK", "root", null);
  
  // 3.      
  java.sql.Statement stmt = conn.createStatement();
  
    //         SQL
  stmt.executeUpdate("insert into MyUser(username, password) values('JDBC     ', '  ')");
  
  
  // 4.    SQL
  java.sql.ResultSet rs = stmt.executeQuery("select * from MyUser");
  
  // 5.           
  while(rs.next()) {
   System.out.println(rs.getInt(1));
   System.out.println(rs.getString("username"));
   System.out.println(rs.getString("password"));
   System.out.println();
  }
  

  // 6.     
  rs.close();
  stmt.close();
  conn.close();

좋은 웹페이지 즐겨찾기