importjava.sql.DriverManager;importjava.sql.Connection;importjava.sql.Statement;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassJDBCExample{publicstaticvo...
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCExample {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.driver");
String url = "jdbc:mysql://localhost:3306/test?user=root&password=123";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from student");
while(rs.next()){
System.out.print("学生的学号: " + rs.getInt(1));
System.out.print("\t学生的名字: " + rs.getString(2));
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
System.out.println("找不到指定的驱动程序类!");
}catch(SQLException e){
e.printStackTrace();
}
}
}
展开