首先这个方法适合初学者数据库连接,下面的只是代码,没有理论,理论到处都是,我把代码给大家,我觉得从代码中找出理论这样的效果很难让你忘记,希望能帮助你,用的是MySQL数据库这个代码
package conn;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class CoD {
public static void main(String[] args) {
try {
//Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("com.mysql.jdbc.Driver");
//String url="jdbc:oracle:thin:@127.0.0.1:1521:ora9";
try {
Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
Statement stmt=(Statement) conn.createStatement();
ResultSet rs=(ResultSet) stmt.executeQuery("select name from book");
while(rs.next()){
System.out.println("\tDname: "+rs.getString("name"));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
阅读(676) | 评论(0) | 转发(0) |