纯Java应用程序连接数据库的方法:
1:首先要到Microsoft里去把驱动程序给下载了,然后解压缩后会有三个文件,列表如下:
MSSQLserver.jar,msutil.jar,msbase.jar文件
然后将上面的三个文件导入Classpath路径中去:
2.然后就可以直接的去使用了:
连接数据库的程序代码如下:
import java.sql.*;
class dbconn
{
public static void main(String[] args)
{
System.out.println("Hello World!");
dbconn aaa=new dbconn();
aaa.connectiondb();
System.out.println("this is ending!");
}
public static void connectiondb ()
{
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://panming:1433;DatabaseName=pubs";
String user="sa";
String passWord="1980";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
String sql="select * from jobs";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
System.out.println("It has some recordset!");
while(rs.next()) {
System.out.println(rs.getString(1));
}
}
else
{
System.out.println("It has no recordset!");
}
}
catch (Exception e)
{System.out.println("OK!!");}
}
}