当前位置: 代码迷 >> Eclipse >> 如何让Java写的程序和SQL2008数据库连接起来
  详细解决方案

如何让Java写的程序和SQL2008数据库连接起来

热度:51   发布时间:2016-04-23 01:08:05.0
怎么让Java写的程序和SQL2008数据库连接起来
毕业设计要求写一个物业管理系统 现在就差程序演示了 
数据库这块老是出问题 我用的是Eclipse和SQL2008怎么让他们连接起来呢 
求大神帮忙 我们后天就要答辩了 小弟感激不尽啊
文件有点大 最好能留个邮箱

连接数据库文件代码如下:
//连接并初始化数据库

import java.sql.*;

import javax.swing.*;
import java.net.*;
import java.io.*;
/**
 * 原始的mssql连接文件
 * @author 字母花园
 *
 */

class mssql_ConnectServer
{
public static Connection connect(String url,String userName,String password) //连接数据库
{
Connection con=null; //为了建立连接而声名的引用
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //ODBC连接数据库
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //JDBC连接数据库
con=DriverManager.getConnection(url,userName,password); //初始化数据库
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"数据库连接错误"); //显示出错对话框
e.printStackTrace();
}
return con;
}

public static void initDataBase() //初始化数据库
{
Connection con=connect("jdbc:odbc:yzgl","",""); //ODBC初始化数据库
//Connection con=connect("jdbc:microsoft:sqlserver://localhost:1433;databasename=master","sa",""); //JDBC初始化数据库
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc://127.0.0.1:1433/yzgl";
String userName = "aaaa";
String password = "1111";
Statement stmt=null; //可执行SQL语句的引用
ResultSet rs=null; //执行SQL语句返回结果的引用
try
{
if(con!=null) //如果连接数据库成功
{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //创建SQL语句,设置窗口光标上下可以滚动
rs=stmt.executeQuery("select * from sysdatabases where name='yzgl'"); //执行SQL语句
if(!rs.next()) //next()代表光标往下一条记录,如果有内容那么为真,加上!就为假,那么就不建库了;如果没内容那就为假,加上!就为真,那么就可以建库了
{
URL url1=ClassLoader.getSystemResource("");
File file=new File(url1.getPath());
String dir=file.getAbsolutePath();
String sql="create database yzgl on primary(name='yzgl_data',filename='"
+dir+"\\..\\database\\yzgl_data.mdf',size=1,maxsize=50,filegrowth=10%) log on (name='yzgl_log',filename='"
+dir+"\\..\\database\\yzgl_log.ldf',size=1,maxsize=UNLIMITED,filegrowth=10%)";
stmt.execute(sql); //执行SQL语句
}
}
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
rs.close(); //关闭数据库,倒着关
stmt.close();
con.close();
}
catch(Exception e)
{
}
}


}
public static void initTable() //初始化表
{
Connection con=connect("jdbc:odbc:yzgl","","");
//Connection con=connect("jdbc:microsoft;sqlserver://localhost:1433;databasename=yzgl","sa","");
Statement stmt=null;
ResultSet rs=null;
try
{
if(con!=null)
{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("select * from yzgl where name='house'");
if(!rs.next())
{
String sql="create table house";
sql=sql+"(houseID int primary key,";
sql=sql+"houseingID int not null,houseUnit char(10) not null,houseFloor char(10) not null,houseArea char(10) not null)";
stmt.execute(sql);
}
rs=stmt.executeQuery("select * from sysobjects where name='owner'");
if(!rs.next())
{
String sql="create table owner";
sql=sql+"(ownerID int identity(1,1) primary key,";
sql=sql+"ownerName char(10) not null,ownerSex char(2) not null,";
sql=sql+"ownerTel char(15) not null,ownerAddress char(30) not null,";
sql=sql+"houseID int references house(houseID),moveinDate char(15) not null,memo char(50),carNumber char(5),ownerPhoto image)";
stmt.execute(sql);
}
rs=stmt.executeQuery("select * from yzgl where name='charge1'");
if(!rs.next())
{
String sql="create table charge1";
sql=sql+"(ownerName char(10) primary key,";
sql=sql+"houseID int references house(houseID),chargeName char(20) not null,much char(10) not null,money char(10) not null,date char(12) not null,";
sql=sql+"receiver char(10) not null)";
stmt.execute(sql);
}
rs=stmt.executeQuery("select * from yzgl where name='charge2'");
if(!rs.next())
{
String sql="create table charge2";
sql=sql+"(ownerName char(10) primary key,";
sql=sql+"houseID int references house(houseID),chargeName char(20) not null,much char(10) not null,money char(10) not null,date char(12) not null,";
sql=sql+"receiver char(10) not null)";
stmt.execute(sql);
}
rs=stmt.executeQuery("select * from yzgl where name='charge3'");
if(!rs.next())
  相关解决方案