JAVA mysql数据库连接
package learn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class Example01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String URL="jdbc:mysql://127.0.0.1:3306/test1";
String DRIVER="com.mysql.jdbc.Driver";
String USERNAME="root";
String PASSWD="123456";
String sql;
Connection connection =null;
try{
Class.forName(DRIVER);
connection=DriverManager.getConnection(URL,USERNAME,PASSWD);
Statement stmt = connection.createStatement();
sql = "select * from t1";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
}catch(SQLException e){
System.out.println("test1");
e.printStackTrace();
}catch(ClassNotFoundException e){
System.out.println("test2");
e.printStackTrace();
}finally{
try{
connection.close();
}catch(SQLException e){
System.out.println("test3");
e.printStackTrace();
}
}
}
}