
1、加载驱动程序。
2、创建连接对象。
3、创建sql语句执行对象 。
4、执行sql语句。
5、对执行结果进行处理。
6、关闭相关的连接对象即可(顺序跟声明的顺序相反)。
处理结果两种情况:
1、执行更新返回的是本次 *** 作影响到的记录数。
2、执行查询返回的结果是一个ResultSet对象。
ResultSet包含符合SQL语句中条件的所有行,并且它通过一套get方法提供了对这些 行中数据的访问。
扩展资料:
Statement
要执行SQL语句,必须获得java.sql.Statement实例,Statement实例分为以下3 种类型:
1、执行静态SQL语句。通常通过Statement实例实现。
2、执行动态SQL语句。通常通过PreparedStatement实例实现。
3、执行数据库存储过程。通常通过CallableStatement实例实现。
参考资料:百度百科JAVA
1. 加载一个对应数据库的JDBC驱动在建立到一个数据库的连接之前,必须先加载这个数据库的JDBC驱动程序,加载之后此driver会自动注册到JDBC驱动列表中。加载一个JDBC驱动有两种方法。
a) 在命令行方式下指定驱动器或者用冒号分割驱动器列表:
具体命令如下:
C:\>java –Djdbc.drivers = com.company1.Driver:com.company2.Driver youProject
b)第二种方法,在程序中调用Class.forName()方法。推荐使用。。。。
try
{
String driverName = “com.imaginary.sql.msql.MsqlDriver”
Class.forName(driverName).newInstance()
}
Catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
2.连接到数据库。
根据您后台待连接的数据库不同,而有小小的差别。
a) 连接到Oracle数据库。
Connection connection = null
try
{
//load the jdbc driver
String driverName = “oracle.jdbc.driver.OracleDriver”
Class.forName(driverName).newInstance()
//create a connection to the database
String serverName = “127.0.0.1”
String serverPort = “1521”
String serverID = “datebase1”
String userName = “hello”
String userPsw = “world”
String url = “jdbc:oracle.thin:@” + serverName + “:” + serverPort + “:” + serverID
Connection = DriverManager.getConnection(url , userName , userPsw)
}
catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
catch(SQLException e2)
{
//catch could not connect to the database exception.
}
b) 连接到一个SQL Server数据库。
Connection connection = null
try
{
//load the jdbc driver
String driverName = “com.microsoft.jdbc.sqlserver.SQLServerDriver”
Class.forName(driverName).newInstance()
//create a connection to the database
String serverName = “127.0.0.1”
String serverPort = “1433”
String serverID = serverName + serverPort
String userName = “hello”
String userPsw = “world”
String url = “jdbc:JSQLConnect ://” + serverID
Connection = DriverManager.getConnection(url , userName , userPsw)
}
catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
catch(SQLException e2)
{
//catch could not connect to the database exception.
}
c) 连接到一个MySQL数据库上。。。。
Connection connection = null
try
{
//load the jdbc driver
String driverName = “org.gjt.mm.mysql.Driver”
Class.forName(driverName).newInstance()
//create a connection to the database
String serverName = “127.0.0.1”
String serverID = “database”
String userName = “hello”
String userPsw = “world”
String url = “jdbc:mysql ://” + serverName + “/” + serverID
Connection = DriverManager.getConnection(url , userName , userPsw)
}
catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
catch(SQLException e2)
{
//catch could not connect to the database exception.
}
综合上面的三种数据库连接方式 , 其实大同小异。由于访问不同的数据库和所使用的数据库驱动程序不同,所以导致代码表面上有小小不同,但透过表面看来,内部都是
1. 加载一个特定的数据库JDBC驱动。
2. 连接到一个数据库。
3. 之后,就可以对一个特定的数据库进行特定的 *** 作了。
附上各种数据库的JDBC驱动起可用信息网址:
http://java.sun.com/products/jdbc
对于Oracle数据库,请参考:
http://otn.oracle.com.software/content.html
对于MySQL数据库,请参考:
http://mmMySQL.sourceforge.net
对于SQL Server数据库,有很多的驱动可选,比较常用的:
http://www.microsoft.com/china/sql/downloads/2000/jdbc.asp
http://www.freetds.org
http://www.datadirect-technologies.com
用JAVA连接数据库主要有两种方式,一是用JDBC-ODBC桥来连接,二是用相关厂商提供的相应驱动程序来连接,首先谈谈第一种连接。 \x0d\x0a\x0d\x0aJDBC-ODBC桥接器是用JdbcOdbc.Class和一个用于访问ODBC驱动程序的本地库实现的。对于WINDOWS平台,该本地库是一个动态连接库DLL(JDBCODBC.DLL)。 \x0d\x0a\x0d\x0a由于JDBC在设计上与ODBC很接近。在内部,这个驱动程序把JDBC的方法映射到ODBC调用上,这样,JDBC就可以和任何可用的ODBC驱动程序进行交互了。这种桥接器的优点是,它使JDBC目前有能力访问几乎所有的数据库。通行方式如图所示: \x0d\x0a\x0d\x0a应用程序---JDBC API---JDBC-ODBC---ODBC API---ODBC层---数据源 \x0d\x0a\x0d\x0a具体 *** 作方法为: \x0d\x0a\x0d\x0a首先打开控制面板的管理工具,打开数据源(ODBC),在用户DSN里面添加数据源(即你要连接的数据库的名字),在这里假定连接SQL SERVER 2000的GoodsSupply数据库。名称填写你要连接的数据库的名称(GoodsSupply),然后逐步设置,如果选用了使用SQL-SERVER密码认证的话,就要输入相应的用户名及密码连接到数据库。一路下一步设置完成。 \x0d\x0a\x0d\x0a在JAVA里面编写程序进行测试,在这里我的程序是让用户输入任意的表名与与列名,把该列的所有数据输出。源代码如下: \x0d\x0a\x0d\x0aimport java.io.BufferedReader\x0d\x0aimport java.io.InputStreamReader\x0d\x0aimport java.sql.*\x0d\x0a\x0d\x0apublic class ODBCBridge { \x0d\x0a\x0d\x0apublic static void main(String[] args) { \x0d\x0aString url="jdbc:odbc:GoodsSupply"\x0d\x0aStatement sm=null\x0d\x0aString command=null\x0d\x0aResultSet rs=null\x0d\x0aString tableName=null\x0d\x0aString cName=null\x0d\x0aString result=null\x0d\x0aBufferedReader input=new BufferedReader(new InputStreamReader(System.in))\x0d\x0atry { \x0d\x0atry { \x0d\x0aClass.forName("sun.jdbc.odbc.JdbcOdbcDriver")//加载驱动 \x0d\x0a}catch(ClassNotFoundException e){ \x0d\x0aSystem.out.println("Can not load Jdbc-Odbc Bridge Driver")\x0d\x0aSystem.err.print("ClassNotFoundException:")\x0d\x0aSystem.err.println(e.getMessage())\x0d\x0a} \x0d\x0aConnection con=DriverManager.getConnection(url,"USER","PASSWORD")//使用SQL-SERVER2000认证 \x0d\x0aDatabaseMetaData dmd=con.getMetaData()//DMD为连接的相应情况 \x0d\x0aSystem.out.println("连接的数据库:"+dmd.getURL())\x0d\x0aSystem.out.println("驱动程序:"+dmd.getDriverName())\x0d\x0asm=con.createStatement()\x0d\x0aSystem.out.println("输入表名")\x0d\x0atableName=input.readLine()\x0d\x0awhile(true) { \x0d\x0aSystem.out.println("输入列名(为空时程序结束):")\x0d\x0acName=input.readLine()\x0d\x0aif(cName.equalsIgnoreCase("")) \x0d\x0abreak\x0d\x0acommand="select "+cName+" from "+tableName\x0d\x0ars=sm.executeQuery(command)//执行查询 \x0d\x0aif(!rs.next()) \x0d\x0aSystem.out.println("表名或列名输入有误")\x0d\x0aelse { \x0d\x0aSystem.out.println("查询结果为:")\x0d\x0ado \x0d\x0a{ \x0d\x0aresult=rs.getString(cName)\x0d\x0a//数据库语言设置为中文,不用转换编码 \x0d\x0a//result=new String(result.getBytes("ISO-8859-1"),"GB2312")\x0d\x0aSystem.out.println(result)\x0d\x0a}while(rs.next())\x0d\x0a} \x0d\x0a} \x0d\x0a}catch(SQLException ex) { \x0d\x0aSystem.out.println("SQLException:")\x0d\x0awhile(ex!=null) { \x0d\x0aSystem.out.println("Message:"+ex.getMessage())\x0d\x0aex=ex.getNextException()\x0d\x0a} \x0d\x0a}catch(Exception e) { \x0d\x0aSystem.out.println("IOException")\x0d\x0a} \x0d\x0a} \x0d\x0a}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)