package cn.jbit.jdbctest;
import java.sql.*;
public class TestConnection {
/**Test connect to the database--sqlserlver
* @author Allen
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//载入驱动
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch(ClassNotFoundException e) {
System.err.println("Link to database error!");
}
//连接数据库
try{
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;Databasename=epet","jbit","bdqn");
System.out.println("Database connection build successful!");
} catch(SQLException e) {
System.err.println("Link to database error!");
}
try{
stmt=conn.createStatement();
StringBuffer sbsql=new StringBuffer("select * from dog");//查看操作
stmt.execute("delete dog where id=5");//删除操作
//以下语句是插入操作
stmt.execute("insert into dog(id,name,health,love,strain) values ('5','壮壮','100','100','藏獒')");
stmt.executeUpdate("update dog set health=90,love=90 where id=1");//更新操作
rs=stmt.executeQuery(sbsql.toString());
System.out.println("ID\t"+"昵称\t"+"温饱度\t"+"开心度\t"+"品种");
while(rs.next()){
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getInt(3)+"\t");
System.out.print(rs.getInt(4)+"\t");
System.out.print(rs.getString(5)+"\n");
}
}catch(SQLException e){
e.printStackTrace();
System.err.println("Create statement error!");
}finally {//根据数据库建立是否成功做相应的处理
try{
System.out.println("Database close successful!");
}
} catch(SQLException e) {
System.out.println("Database connection build fail!");
}
}
}
import java.sql.*;
public class TestConnection {
/**Test connect to the database--sqlserlver
* @author Allen
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//载入驱动
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch(ClassNotFoundException e) {
System.err.println("Link to database error!");
}
//连接数据库
try{
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;Databasename=epet","jbit","bdqn");
System.out.println("Database connection build successful!");
} catch(SQLException e) {
System.err.println("Link to database error!");
}
try{
stmt=conn.createStatement();
StringBuffer sbsql=new StringBuffer("select * from dog");//查看操作
stmt.execute("delete dog where id=5");//删除操作
//以下语句是插入操作
stmt.execute("insert into dog(id,name,health,love,strain) values ('5','壮壮','100','100','藏獒')");
stmt.executeUpdate("update dog set health=90,love=90 where id=1");//更新操作
rs=stmt.executeQuery(sbsql.toString());
System.out.println("ID\t"+"昵称\t"+"温饱度\t"+"开心度\t"+"品种");
while(rs.next()){
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getInt(3)+"\t");
System.out.print(rs.getInt(4)+"\t");
System.out.print(rs.getString(5)+"\n");
}
}catch(SQLException e){
e.printStackTrace();
System.err.println("Create statement error!");
}finally {//根据数据库建立是否成功做相应的处理
try{
if(null!=conn){
rs.close();
stmt.close();
System.out.println("Database close successful!");
}
} catch(SQLException e) {
System.out.println("Database connection build fail!");
}
}
}
}
/**
*--------------------------------------------分割符----------------------------------------------------------
**/
java连接SQLserver:
1. Connection conn=null; Statement stmt=null; ResultSet rs=null;
2. Class.forName(JDBC驱动类的名称);
3. conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;Databasename=数据库名","数据库用户名","数据库用户密码");
4. stmt = conn.createStatement
5. stmt.execute系列指令(数据库查询,插入,删除等操作)