MySQL:
String Driver="com.mysql.jdbc.Driver"; //驱动程序
String URL="jdbc:mysql://localhost:3306/db_name";//URL,db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Microsoft SQL Server驱动(msbase.jar、mssqlserver.jar、msutil.jar):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name";
String Username="username";
String Password="password";
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,UserName,Password);
Microsoft SQL Server驱动(jtds-1.2.jar):
String Driver="net.sourceforge.jtds.jdbc.Driver";
String URL="jdbc:jtds:sqlserver://localhost:1433/db_name;s=8.0;lastupdatecount=true";
String Username="username";
String Password="password";
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,UserName,Password);
Sysbase:
String Driver="com.sybase.jdbc.SybDriver"; //驱动程序
String URL="jdbc:Sysbase://localhost:5007/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Sysbase:
String url="jdbc:sybase:Tds:localhost:5007/tsdata";
Properties sysProps=System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn=DriverManager.getConnection(url,SysProps);
Oracle(用thin模式):
String Driver="oracle.jdbc.driver.OracleDriver"; //连接数据库的方法
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //orcl为数据库的SID
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance(); //加载数据库驱动
Connection con=DriverManager.getConnection(URL,Username,Password);
PostgreSQL:
String Driver="org.postgresql.Driver"; //连接数据库的方法
String URL="jdbc:postgresql://localhost/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
DB2:
String Driver="com.ibm.db2.jdbc.app.DB2.Driver"; //连接具有DB2客户端的Provider实例
//String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; //连接不具有DB2客户端的Provider实例
String URL="jdbc:db2://localhost:5000/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Informix:
String Driver="com.informix.jdbc.IfxDriver";
String URL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
JDBC-ODBC:
String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
String URL="jdbc:odbc:dbsource"; //dbsource为数据源名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
4 建立Statement对象或PreparedStatement对象.例如:
//建立Statement对象
Statement stmt=conn.createStatement();
//建立ProparedStatement对象
String sql="select * from user where userName=? and password=?";
PreparedStatement pstmt=Conn.prepareStatement(sql);
pstmt.setString(1,"admin");
pstmt.setString(2,"liubin");
5 执行SQL语句.例如:
String sql="select * from users";
ResultSet rs=stmt.executeQuery(sql);
//执行动态SQL查询
ResultSet rs=pstmt.executeQuery();
//执行insert update delete等语句,先定义sql
stmt.executeUpdate(sql);
6 访问结果记录集ResultSet对象。例如:
while(rs.next)
{
out.println("你的第一个字段内容为:"+rs.getString());
out.println("你的第二个字段内容为:"+rs.getString(2));
}
7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭,释放所占用的资源.例如:
rs.close();
stmt.clost();
pstmt.close();
con.close();
String Driver="com.mysql.jdbc.Driver"; //驱动程序
String URL="jdbc:mysql://localhost:3306/db_name";//URL,db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Microsoft SQL Server驱动(msbase.jar、mssqlserver.jar、msutil.jar):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name";
String Username="username";
String Password="password";
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,UserName,Password);
Microsoft SQL Server驱动(jtds-1.2.jar):
String Driver="net.sourceforge.jtds.jdbc.Driver";
String URL="jdbc:jtds:sqlserver://localhost:1433/db_name;s=8.0;lastupdatecount=true";
String Username="username";
String Password="password";
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,UserName,Password);
Sysbase:
String Driver="com.sybase.jdbc.SybDriver"; //驱动程序
String URL="jdbc:Sysbase://localhost:5007/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Sysbase:
String url="jdbc:sybase:Tds:localhost:5007/tsdata";
Properties sysProps=System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn=DriverManager.getConnection(url,SysProps);
Oracle(用thin模式):
String Driver="oracle.jdbc.driver.OracleDriver"; //连接数据库的方法
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //orcl为数据库的SID
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance(); //加载数据库驱动
Connection con=DriverManager.getConnection(URL,Username,Password);
PostgreSQL:
String Driver="org.postgresql.Driver"; //连接数据库的方法
String URL="jdbc:postgresql://localhost/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
DB2:
String Driver="com.ibm.db2.jdbc.app.DB2.Driver"; //连接具有DB2客户端的Provider实例
//String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; //连接不具有DB2客户端的Provider实例
String URL="jdbc:db2://localhost:5000/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Informix:
String Driver="com.informix.jdbc.IfxDriver";
String URL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
JDBC-ODBC:
String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
String URL="jdbc:odbc:dbsource"; //dbsource为数据源名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
4 建立Statement对象或PreparedStatement对象.例如:
//建立Statement对象
Statement stmt=conn.createStatement();
//建立ProparedStatement对象
String sql="select * from user where userName=? and password=?";
PreparedStatement pstmt=Conn.prepareStatement(sql);
pstmt.setString(1,"admin");
pstmt.setString(2,"liubin");
5 执行SQL语句.例如:
String sql="select * from users";
ResultSet rs=stmt.executeQuery(sql);
//执行动态SQL查询
ResultSet rs=pstmt.executeQuery();
//执行insert update delete等语句,先定义sql
stmt.executeUpdate(sql);
6 访问结果记录集ResultSet对象。例如:
while(rs.next)
{
out.println("你的第一个字段内容为:"+rs.getString());
out.println("你的第二个字段内容为:"+rs.getString(2));
}
7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭,释放所占用的资源.例如:
rs.close();
stmt.clost();
pstmt.close();
con.close();