1.load the appropriate Driver class,which has the side effect of registering with the DriverManager. you can use: Class.forClass(); 2.get a Connection object,for example Connection con = DriverManager.getConnection(dbURL,name,password); 3.get a Statement object Statement st = con.createStatement(); 4.get a ResultSet object,using the Statement object's executeQuery(); ResultSet rs = st.executeQuery("select * from MyTable); 5.iterate over the ResultSet 6.while(rs.next()){ int x = rs.getInt("CustNO"); 7.close the ResultSet. 8.close the Statement; 9.close the Connection
the DriverManager.
you can use: Class.forClass();
2.get a Connection object,for example
Connection con = DriverManager.getConnection(dbURL,name,password);
3.get a Statement object
Statement st = con.createStatement();
4.get a ResultSet object,using the Statement object's executeQuery();
ResultSet rs = st.executeQuery("select * from MyTable);
5.iterate over the ResultSet
6.while(rs.next()){
int x = rs.getInt("CustNO");
7.close the ResultSet.
8.close the Statement;
9.close the Connection
String dbAdress = dbAdressText.getText();
String dsName = dsNameText.getText();
String userName = userNameText.getText();
char [] pw = password.getPassword();
StringBuffer buf1 = new StringBuffer();
buf1.append(pw);
String passwordString = buf1.toString();
buf1 = null; StringBuffer buf2 = new StringBuffer("jdbc:db2//");
buf2.append(dbAdress);
buf2.append("/");
buf2.append(dsName);
String dburl = new String();
dburl = buf2.toString();
buf2 = null; try{
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");
Connection con = DriverManager.getConnection(dburl,userName,passwordString);
Statement statement = con.createStatement();
statement.executeUpdate(OperationText.getText());
System.out.println("ok");
con.close();
}
catch(Exception exception){
System.out.println(OperationText.getText());
exception.printStackTrace();
System.out.println(exception.getMessage());
JOptionPane.showMessageDialog(this,"数据库出错");
}