java数据库代码,java调用数据库代码

求Java数据库代码注释

代码主要列出连接数据库的关键代码,其他访问数据库代码省略

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站建设、外贸营销网站建设、吴兴网络推广、重庆小程序开发、吴兴网络营销、吴兴企业策划、吴兴品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供吴兴建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

1、Oracle8/8i/9i数据库(thin模式)

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

String url="jdbc:oracle:thin:@localhost:1521:orcl";

//orcl为数据库的SID

String user="test";

String password="test";

Connection conn= DriverManager.getConnection(url,user,password);

2、DB2数据库

Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();

String url="jdbc:db2://localhost:5000/sample";

//sample为你的数据库名

String user="admin";

String password="";

Connection conn= DriverManager.getConnection(url,user,password);

3、Sql Server7.0/2000数据库

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

//mydb为数据库

String user="sa";

String password="";

Connection conn= DriverManager.getConnection(url,user,password);

4、Sybase数据库

Class.forName("com.sybase.jdbc.SybDriver").newInstance();

String url =" jdbc:sybase:Tds:localhost:5007/myDB";

//myDB为你的数据库名

Properties sysProps = System.getProperties();

SysProps.put("user","userid");

SysProps.put("password","user_password");

Connection conn= DriverManager.getConnection(url, SysProps);

5、Informix数据库

Class.forName("com.informix.jdbc.IfxDriver").newInstance();

String url =

"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;

user=testuser;password=testpassword";

//myDB为数据库名

Connection conn= DriverManager.getConnection(url);

6、MySQL数据库

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

String url ="jdbc:mysql://localhost/myDB?user=softpassword=soft1234useUnicode=truecharacterEncoding=8859_1"

//myDB为数据库名

Connection conn= DriverManager.getConnection(url);

7、PostgreSQL数据库

Class.forName("org.postgresql.Driver").newInstance();

String url ="jdbc:postgresql://localhost/myDB"

//myDB为数据库名

String user="myuser";

String password="mypassword";

Connection conn= DriverManager.getConnection(url,user,password);

java连接数据库的代码

用这个类吧.好的话,给我加加分.

import java.sql.*;

/**

* @功能: 一个JDBC的本地化API连接类,封装了数据操作方法,只用传一个SQL语句即可

* @作者: 李开欢

* @日期: 2007/

*/

public class ConnectionDemo {

/*

* 这里可以将常量全部放入另一个类中,以方便修改

*/

private static Connection conn;

private static Statement ps;

private static ResultSet rs;

private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

private static final String USER ="sa";

private static final String PASS = "sa";

public ConnectionDemo() {

// TODO Auto-generated constructor stub

ConnectionDemo.getConnection();

}

public static Connection getConnection(){

System.out.println("连接中...");

try {

Class.forName(ConnectionDemo.DRIVER);

conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);

System.out.println("成功连接");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

public static Statement getStatement(String sql){

System.out.println("执行SQL语句中...");

try {

ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

if(sql.substring(0, 6).equals("select")){

rs = ps.executeQuery(sql);

System.out.println("执行完查询操作,结果已返回ResultSet集合");

}else if(sql.substring(0, 6).equals("delete")){

ps.executeUpdate(sql);

System.out.println("已执行完毕删除操作");

}else if(sql.substring(0, 6).equals("insert")){

ps.executeUpdate(sql);

System.out.println("已执行完毕增加操作");

}else{

ps.executeUpdate(sql);

System.out.println("已执行完毕更新操作");

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return ps;

}

public static ResultSet getResultSet(){

System.out.println("查询结果为:");

return rs;

}

public static void closeConnection(){

System.out.println("关闭连接中...");

try {

if (rs != null) {

rs.close();

System.out.println("已关闭ResultSet");

}

if (ps != null) {

ps.close();

System.out.println("已关闭Statement");

}

if (conn != null) {

conn.close();

System.out.println("已关闭Connection");

}

} catch (Exception e) {

// TODO: handle exception

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

ConnectionDemo.getConnection();

String sql = "delete from type where id = 1";

ConnectionDemo.getStatement(sql);

String sql2 = "insert into type values(1,'教学设备')";

ConnectionDemo.getStatement(sql2);

String sql1 = "select * from type";

ConnectionDemo.getStatement(sql1);

ResultSet rs = ConnectionDemo.getResultSet();

System.out.println("编号 "+"类 型");

try {

while(rs.next()){

System.out.print(" "+rs.getInt(1)+" ");

System.out.println(rs.getString(2));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ConnectionDemo.closeConnection();

}

}

用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驱动起可用信息网址:

对于Oracle数据库,请参考:

对于MySQL数据库,请参考:

对于SQL Server数据库,有很多的驱动可选,比较常用的:

java连接sqlserver数据库代码

%

String sqlDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";

String url="jdbc:sqlserver://localhost:1433;DatabaseName=自己的数据库名字";

String user="用户名";

String password="密码";

Connection conn=null;

try{

Class.forName(sqlDriver).newInstance();

conn=DriverManager.getConnection(url,user,password);

// out.println("数据库加载成功");

}catch(Exception e){

// out.println("数据库加载出现错误!");

}

%

一段java中数据库代码解释

StringBuffer

sqlq=new

StringBuffer("

SELECT

*

FROM

")

;//申明一个可变字符串

,要存了一个sql语句,并且由"

SELECT

*

FROM

"可知其为一个select查询语句

sqlq.append(DtoMapGroupOptions.DB_TABLE_NAME)

;//DtoMapGroupOptions.DB_TABLE_NAME应该是一个字符串,字面值为一个表的名称,要在这个表里查数据

sqlq.append("

ORDER

BY

")

;//这个制定查出来的结果集需要排序

sqlq.append(DtoMapGroupOptions.COLUMN_optionID)

;//DtoMapGroupOptions.COLUMN_optionID应该是某一列的列名,根据这一列来排序,如果这一列是数字,那么就会根据数字大小排,字符串可能按abc排,和excel排序时一样的,即根据某一列来扩展至整个区域排序

sqlq.append("

DESC

")

;//这个事制定按降序还是升序,这里是降序

//后面的语句要看上下文,那个pb不知是什么

ListRow

list

=

null

;

pb.isRequireTotalRow(true);

String

sqlStr=sqlq.toString();

list

=

pb.getInfo(sqlStr,

null,

DtoMapGroupOptions.DATA_SOURCE_ID);//可能是把结果集放入list中,根据sqlStr中的sql语句

JAVA连接数据库连接代码怎么写?

1 将数据库的JDBC驱动加载到classpath中,在基于JAVAEE的WEB应用实际开发过程中,通常要把目标数据库产品的JDBC驱动复制到WEB-INF/lib下.

2 加载JDBC驱动,并将其注册到DriverManager中,下面是一些主流数据库的JDBC驱动加裁注册的代码:

//Oracle8/8i/9iO数据库(thin模式)

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

//Sql Server7.0/2000数据库

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

//DB2数据库

Class.froName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();

//Informix数据库

Class.forName("com.informix.jdbc.IfxDriver").newInstance();

//Sybase数据库

Class.forName("com.sybase.jdbc.SybDriver").newInstance();

//MySQL数据库

Class.forName("com.mysql.jdbc.Driver").newInstance();

//PostgreSQL数据库

Class.forNaem("org.postgresql.Driver").newInstance();

3 建立数据库连接,取得Connection对象.例如:

//Oracle8/8i/9i数据库(thin模式)

String url="jdbc:oracle:thin:@localhost:1521:orcl";

String user="scott";

String password="tiger";

Connection conn=DriverManager.getConnection(url,user,password);

--完整的太多了!我已经把完整的代码发到你QQ邮箱了!

当前名称:java数据库代码,java调用数据库代码
URL地址:https://www.cdcxhl.com/article38/hcgosp.html

成都网站建设公司_创新互联,为您提供外贸建站ChatGPTGoogle微信小程序品牌网站设计外贸网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设