【原创】大叔经验分享(94)jdbc连接mysql、sqlserver、oracle
作者:互联网
Mysql
driver下载:https://mvnrepository.com/artifact/mysql/mysql-connector-java
import java.sql.*; public class SqlServerTest { public static void main(String []args) { String driverName="com.mysql.jdbc.Driver"; String dbURL="jdbc:mysql://$ip:3306/$db"; String userName="$user"; String userPwd="$password"; try { Class.forName(driverName); Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd); Statement st = dbConn.createStatement(); ResultSet rs = st.executeQuery("select * from user"); while (rs.next()) { System.out.println(rs.getString(1)); } System.out.println("success!"); } catch(Exception e) { e.printStackTrace(); System.out.print("fail!"); } } }
SqlServer
driver下载:https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4/4.0
import java.sql.*; public class SqlServerTest { public static void main(String []args) { String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL="jdbc:sqlserver://$ip:1433;DatabaseName=$db"; String userName="$user"; String userPwd="$password"; try { Class.forName(driverName); Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd); Statement st = dbConn.createStatement(); ResultSet rs = st.executeQuery("select * from sysobjects where xtype='U'"); while (rs.next()) { System.out.println(rs.getString(1)); } System.out.println("success!"); } catch(Exception e) { e.printStackTrace(); System.out.print("fail!"); } } }
Oracle
driver下载:https://www.oracle.com/database/technologies/jdbc-drivers-12c-downloads.html
import java.sql.*; public class SqlServerTest { public static void main(String []args) { String driverName="oracle.jdbc.driver.OracleDriver"; String dbURL="jdbc:oracle:thin:@//$ip:1521/$db"; String userName="$user"; String userPwd="$password"; try { Class.forName(driverName); Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd); Statement st = dbConn.createStatement(); ResultSet rs = st.executeQuery("select * from all_tab_comments"); while (rs.next()) { System.out.println(rs.getString(1)); } System.out.println("success!"); } catch(Exception e) { e.printStackTrace(); System.out.print("fail!"); } } }
标签:jdbc,dbURL,String,rs,sqlserver,System,mysql,oracle,out 来源: https://www.cnblogs.com/barneywill/p/12069149.html