myeclipse自带的数据库两种连接方式

本文详细介绍了如何在MyEclipse环境下,通过MyEclipseDerby组件与Tomcat服务器配合,实现与Derby数据库的连接。包括导入相关JAR包、设置数据库连接参数以及数据库操作的基本步骤。
/**客户机/服务器模式 需要开启Myeclipse上和Tomacat在一起的Myeclipse Derby需要启动*/
public class DBConnection {
static PreparedStatement  ps=null;

public static Connection getConnection()  {
Connection con=null;
try {
//导入derbyclient.jar
Class.forName("org.apache.derby.jdbc.ClientDriver");
con=DriverManager.getConnection("jdbc:derby://127.0.0.1:1527/ys2;create=true","root", "1234"); 

      
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;

}

 




}




/**内嵌模式*/
public class DBConnection2 {
static int i=1;
static Statement statement=null;
static ResultSet rs=null;
static PreparedStatement ps;
static String sql="create table ys(id int not null generated always as identity constraint pk primary key, name varchar(50), salary double default null )";
static String insert_sql="insert into ys(name,salary) values('ys',2000)";
public static Connection getConnection2()  {

Connection con=null;
try {
//导入derby.jar
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
// con=DriverManager.getConnection("jdbc:derby:内嵌数据库的位置;create=true(不存在就创建)","root", "1234"); 
con=DriverManager.getConnection("jdbc:derby:D:\\Workspaces02\\Myeclipse自带的Derby数据库\\ys","root", "1234");  
 
//判断数据库表是否存在
  DatabaseMetaData meta = con.getMetaData();
  ResultSet rsTables = meta.getTables(null , null,"YS", null);
  if(rsTables.next()){
  System.out.println("表存在");
  return con;
  }else{
  System.out.println("表不存在");
  createTable(con);
return con;
  }
 
 
 
 
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;

}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值