import java.sql.*;
import java.net.*;public class sqltry
{
public static void main (String[] args)
{
String sDBDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test;user=lysander;password=";
Connection dbconn=null;
try
{
Class.forName(sDBDriver);
dbconn=DriverManager.getConnection(dbURL);
System.out.println("Connection Successful!");
sqltry.executeUpdateStatement(dbconn);
}
catch (Exception e)
{ e.printStackTrace(); }
}
public static void executeUpdateStatement(Connection con)
{ try
{
String SQL = "SELECT * FROM [test].[dbo].[学校代码表] ORDER BY 最低分数";
String SQL2 ="INSERT INTO [test].[dbo].[学校代码表]([学校代码],[学校名],[招生人数],[最低分数]) VALUES (001,ybo1,2000,469)";
Statement stmt = con.createStatement();
int count = stmt.executeUpdate(SQL2);
//关了Statement
stmt.close();
System.out.println("use test datebase "+"ROWS AFFECTED: " + count);
//再建一Statement,下边的ResultSet用这个得到
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(SQL);
while(rs.next())
{
System.out.println(rs.getString("学校名") + ", " + rs.getString("学校代码"));
}
//关闭后来建的Statement
st.close();
rs.close();
}
catch (Exception e)
{e.printStackTrace();}
} //public sqltry()
//{
//}}
帐户应该没有问题,classpath我也设置了,按照帮助文件里面的,我用的是ide是Jcreator,没有设置,但是,我在命令行里编译运行也是不行.我用的是sql server2005
不是2000~~~~~拜托有没有详细的教程,本人菜鸟.有没有高人,留个qq msn交流一下啊~~~谢谢了,顶贴也有分数啊
import java.net.*;public class sqltry
{
public static void main (String[] args)
{
String sDBDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test;user=lysander;password=";
Connection dbconn=null;
try
{
Class.forName(sDBDriver);
dbconn=DriverManager.getConnection(dbURL);
System.out.println("Connection Successful!");
sqltry.executeUpdateStatement(dbconn);
}
catch (Exception e)
{ e.printStackTrace(); }
}
public static void executeUpdateStatement(Connection con)
{ try
{
String SQL = "SELECT * FROM [test].[dbo].[学校代码表] ORDER BY 最低分数";
String SQL2 ="INSERT INTO [test].[dbo].[学校代码表]([学校代码],[学校名],[招生人数],[最低分数]) VALUES (001,ybo1,2000,469)";
Statement stmt = con.createStatement();
int count = stmt.executeUpdate(SQL2);
//关了Statement
stmt.close();
System.out.println("use test datebase "+"ROWS AFFECTED: " + count);
//再建一Statement,下边的ResultSet用这个得到
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(SQL);
while(rs.next())
{
System.out.println(rs.getString("学校名") + ", " + rs.getString("学校代码"));
}
//关闭后来建的Statement
st.close();
rs.close();
}
catch (Exception e)
{e.printStackTrace();}
} //public sqltry()
//{
//}}
帐户应该没有问题,classpath我也设置了,按照帮助文件里面的,我用的是ide是Jcreator,没有设置,但是,我在命令行里编译运行也是不行.我用的是sql server2005
不是2000~~~~~拜托有没有详细的教程,本人菜鸟.有没有高人,留个qq msn交流一下啊~~~谢谢了,顶贴也有分数啊
这是我一贯些连接类法你可以看看package com.superstore.db;
import java.sql.*;public class DBConnection {
private static final String DRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
private static final String URL="jdbc:odbc:superstore";//就jdbc:odbc:数据源
private Connection con=null;
public DBConnection() {
try {
Class.forName(DRIVER);
try {
con = DriverManager.getConnection(URL);
} catch (SQLException ex1) {
System.out.println("数据源错误:");
ex1.printStackTrace();
}
} catch (ClassNotFoundException ex) {
System.out.println("加载驱动错误:");
ex.printStackTrace();
}
}
public Connection getConnection(){
return con;
}
public void close(){
try {
con.close();
} catch (SQLException ex) {
System.out.println("关闭连接时错误:");
ex.printStackTrace();
}
}
/*测试
public static void main(String [] args){
DBConnection db=new DBConnection();
db.close();
}
*/
}//测试的一部分你可以在实现类中
new 对象:
DBConnection db=new DBConnection();
db.getConnection();得到连接
db.close();关闭连接
原帖及讨论:http://bbs.bc-cn.net/dispbbs.asp?boardid=12&id=140292* 最近因为开发活动需要,用上了Eclipse,并要求使用精简版的SQL数据库(即SQL Server 2005)来进行开发项目 *1.准备工作: 准备相关的软件(Eclipse除外,开源软件可以从官网下载)<1>.Microsoft SQL Server 2005 Express Edition下载地址:http://download.microsoft.com/download/0/9/0/09020fab-d2c3-4a8c-b9e0-db53a7a30ae8/SQLEXPR_CHS.EXE<2>.SQL Server Management Studio下载地址:http://www.microsoft.com/downloads/details.aspx?displaylang=zh- cn&FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796#filelist<3>.SQL Server 2005 driver for JDBC下载地址:http://download.microsoft.com/download/8/B/D/8BDABAE2-B6EA-41D4-B903-7916EF3690EF/sqljdbc_1.2.2323.101_enu.exe2.都下载完之后开始进行安装 ,前两个是属于数据库软件,正常安装即可(注意数据库登陆不要使用windows验证)<1> 将JDBC解压缩到任意位置,比如解压到C盘program files下面,并在安装目录里找到sqljdbc.jar文件,得到其路径开始配置环境变量在环境变量classpath 后面追加 C:\Program Files\Microsoft SQL Server2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar<2> 设置SQLEXPRESS服务器:a.打开SQL Server Configuration Manager -> SQLEXPRESS的协议 -> TCP/IPb.右键单击启动TCP/IPc.双击进入属性,把IP地址中的IP all中的TCP端口设置为1433d.重新启动SQL Server 2005服务中的SQLEXPRESS服务器e.关闭SQL Server Configuration Manager<3> 打开刚刚安装好的 SQL Server Management Studio,连接SQLEXPRESS服务器, 新建数据库,起名字为sample<4> 打开Eclipsea.新建工程-> java -> Java project,起名为Testb.选择eclipse->窗口->首选项->Java->installed JRE 编辑已经安装好的jdk,查找目录添加sqljdbc.jarc.右键单击目录窗口中的Test, 选择Build Path ->Configure Build Path..., 添加扩展jar文件,即把sqljdbc.jar添加到其中<5> 编写Java代码来测试连接数据库程序代码:
import Java.sql.*;public class Test {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=sample"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "123456"; //密码
Connection dbConn;try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}
注:1.因为SQLexpress服务器默认是禁用的并且端口号没有配置,所以要进行重新设置2.如果你以前用Java连接SQL Server 2000的话就要注意了:在SQL Server 2000 中加载驱动和URL路径的语句是String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=sample";而SQL Server 2005 中加载驱动和url的语句则为String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=sample";如果写法错误将会找不到驱动.
好了,应该没什么疑问了吧...偶是有连接失败经历才分享给大家的...
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;public final class DBConnection {
private final static String defaultDriverClass="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private final static String defaultDbUrl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=";
/**
* Use it to get a database connection!
* @param dbName database name
* @param user database user name
* @param passwd password
* @return connection
*/
public static Connection getConnection(String dbName,String user,String passwd ){
Connection con=null;
try {
Class.forName(defaultDriverClass);
con=DriverManager.getConnection(defaultDbUrl+dbName,user,passwd);
} catch (SQLException e) {
System.out.println("Create connecton error!");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("Driver class not find!");
e.printStackTrace();
}
return con;
}
/**
* Use the connection to create a satement,the default fecth size is 200
* @param con a database connection
* @return a statement
*/
public static Statement getStatement(Connection con){
Statement st=null;
try {
st=con.createStatement();
st.setFetchSize(200);
} catch (SQLException e) {
System.out.println("Create statement exception!");
e.printStackTrace();
}
return st;
}}写了一个你用用,注意把sql的驱动包放到classpath中去.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at QueryRecord.showRecord(QueryRecord.java:24)
at QueryRecord.main(QueryRecord.java:50)
Exception in thread "main" java.lang.NullPointerException
at QueryRecord.showRecord(QueryRecord.java:40)
at QueryRecord.main(QueryRecord.java:50)
我的数据库连接提示这个是什么错误呀,请高手指导?谢谢
代码如下:
import java.sql.*;
public class QueryRecord {
private Connection conn=null; // 声明连接
private Statement st=null; // 声明Statement对象
private ResultSet rs=null; // 声明结果集对象
/**
* 定义构造方法,用于加载数据库驱动
*/
public QueryRecord(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); // 加载数据库驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 在控制台输出数据表中信息的方法
*/
public void showRecord(){
try {
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_test"; // 数据库db_test的URL
String username="sa"; // 数据库的用户名
String password="123456"; // 数据库密码
conn = DriverManager.getConnection(url, username, password); // 建立连接
st=conn.createStatement(); // 创建Statement对象
String sql="select * from tb_employee"; // 定义SQL查询语句
rs=st.executeQuery(sql); // 执行SQL语句获得结果集
// 如果rs.next()的值为true,即结果集中存在下一条记录,则执行循环体
while (rs.next()){
System.out.print(rs.getInt(1)+"\t"); // 获得当前记录中第1个字段的值并输出
System.out.print(rs.getString(2)+"\t"); // 获得当前记录中第2个字段的值并输出
System.out.print(rs.getString(3)+"\t"); // 获得当前记录中第3个字段的值并输出
System.out.print(rs.getInt(4)+"\t"); // 获得当前记录中第4个字段的值并输出
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close(); // 关闭结果集对象
st.close(); // 关闭Statement对象
conn.close(); // 关闭连接对象
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
QueryRecord query=new QueryRecord(); // 创建类的实例
query.showRecord(); // 调用方法在控制台输出结果集中的记录
}
}