常用的数据库连接语句
一:Mysql的数据库连接语句
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection.("jdbc:mysql://localhost:3306/bookinfo","root","123");
Statement st = con.createStatement();
String sql = "select * from book";
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
}
二:sqlserver的数据库连接语句
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection.("jdbc:microsoft:sqlserver://localhost:1433;databaseName=bookinfo","sa","123");
PreparedStatement ps = con.prepareStatement("select * from book");
ResultSet rs = ps.executeQuery();
while(rs.next()){
out.println(rs.getString(2));
}
三:sqlserver2005的数据库连接语句
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection.("jdbc:sqlserver://localhost:1433;databaseName=bookinfo","sa","123");
PreparedStatement ps = con.prepareStatement("select * from book");
ResultSet rs = ps.executeQuery();
while(rs.next()){
out.println(rs.getString(2));
}
四:oracle的数据库连接语句
jdbc:oracle:thin:@<server>[:<1521>]:<database_name>
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection.("jdbc:oracle:thin:@localhost:1521:bookinfo","zhangsan","123");
PreparedStatement ps = con.prepareStatement("select * from book");
ResultSet rs = ps.executeQuery();
while(rs.next()){
out.println(rs.getString(2));
}