java 联接操作mysql

import  java.sql. * ;
import  java.lang. * ;
import  com.mysql.jdbc.Driver;
public   class  javaToDoMysql {
    
public   static  java.sql.Statement connect()  throws  Exception {
        String location 
=   " 127.0.0.1 " ;
        String database 
=   " hongfu " ;
        String root 
=   " root " ;
        String passwd 
=   " root " ;
        String connName 
=   " jdbc:mysql:// " + location + " :3306/ " + database + " ?user= " + root + " &password= " + passwd;
        Class.forName(
" com.mysql.jdbc.Driver " ).newInstance();
        Connection connection 
=  DriverManager.getConnection(connName);
        Statement st 
=  connection.createStatement();
        
return  st;
    }
    
public   static   void  query()  throws  Exception {
        Statement dbh 
=  connect();
        ResultSet rs 
=  dbh.executeQuery( " select id,name from test " );
        
while (rs.next()) {
            System.out.println(rs.getString(
" id " ) + " , " + rs.getString( " name " ));
        }
    }
    
public   static   void  main(String[] args)  throws  Exception {
        query();
    }

}
 

你可能感兴趣的:(java,mysql,exception,database,query,import)