jdbc登陆查询
我用的mysql
需要导入jar包:mysql-jar\mysql-connector-java-5.1.47-bin.jar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package slect; import java.sql.*;
public class Select { public static void main(String[] args){ String url = "jdbc:mysql://localhost:3306/大吉大利"; String user = "root"; String password = "123456"; try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed()) System.out.println("登陆成功"); Statement statement = conn.createStatement(); String sql = "select * from persion"; ResultSet rs = statement.executeQuery(sql); while(rs.next()) { String id=rs.getString(1); String name=rs.getString(2); System.out.println(id+name); } rs.close(); conn.close(); } catch(Exception e) { e.printStackTrace(); } } }
|