JDBC 基本流程:
1. 加载驱动
a. SQLSERVER:Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
b.ORACLE :Class.forName("oracle.jdbc.driver.oracleDriver");
c.MySQL :Class.forName("com.mysql.jdbc.Driver");
2.得到连接
a.SQLSERVER: DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=UserManager",userName,password);
b.ORACLE :DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:DBTEST",userName,password);
c.MySQl :DriverManager.getConnection("jdbc:mysql://localhost:3306/UserManager",userName,password);
3.创建PreparedStatement
4.执行操作
5.根据结果做处理
例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; 2 String dbURL="jdbc:sqlserver://localhost:1433; DatabaseName=UserManager"; 3 String sqlUserName="sa"; 4 String userPwd="wfjts12332100"; 5 Connection dbConn=null; 6 ResultSet rs=null; 7 PreparedStatement ps=null; 8 try 9 { 10 Class.forName(driverName); 11 dbConn=DriverManager.getConnection(dbURL,sqlUserName,userPwd); 12 System.out.println("Connection Successful!"); 13 ps=dbConn.prepareStatement("select * from users where id=? and passwd=? "); 14 ps.setObject(1, userName); 15 ps.setObject(2, password); 16 rs=ps.executeQuery(); 17 if(rs.next()) 18 { 19 System.out.println("用戶合法"); 20 } 21 } 22 catch(Exception ex) 23 { 24 ex.printStackTrace(); 25 }