刚开始学JAVA,自己按着网上写的
//jdbc_sql简单的数据库连接
import java.sql.*;
//import java.sql.DriverManager;
public class test2 {
public static void main(String args[]){
String url="jdbc:sqlserver://localhost:1433;";
String user="sa";
String password="123456";
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载驱动
con=DriverManager.getConnection(url,user,password);//建立连接
con.setCatalog("login");//选择你要连接的数据库
// 简单数据库操作
stmt=con.createStatement();
stmt.executeUpdate("drop table table_1");
stmt.executeUpdate("create table table_1(id integer,age integer)");
rs=stmt.executeQuery("select * from userTable");
System.out.println("Result before exexuteupdate");
while(rs.next()){
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
}
stmt.executeUpdate("Update userTable set password='asdsa' where username='husan'");
stmt.executeUpdate("Insert into userTable(username,password) values('dida','123as')");
rs=stmt.executeQuery("select * from userTable");
System.out.println("Result after exexuteupdate");
while(rs.next()){
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
}
rs.close();
stmt.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
//jdbc_sql简单的数据库连接
import java.sql.*;
//import java.sql.DriverManager;
public class test2 {
public static void main(String args[]){
String url="jdbc:sqlserver://localhost:1433;";
String user="sa";
String password="123456";
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载驱动
con=DriverManager.getConnection(url,user,password);//建立连接
con.setCatalog("login");//选择你要连接的数据库
// 简单数据库操作
stmt=con.createStatement();
stmt.executeUpdate("drop table table_1");
stmt.executeUpdate("create table table_1(id integer,age integer)");
rs=stmt.executeQuery("select * from userTable");
System.out.println("Result before exexuteupdate");
while(rs.next()){
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
}
stmt.executeUpdate("Update userTable set password='asdsa' where username='husan'");
stmt.executeUpdate("Insert into userTable(username,password) values('dida','123as')");
rs=stmt.executeQuery("select * from userTable");
System.out.println("Result after exexuteupdate");
while(rs.next()){
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
}
rs.close();
stmt.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}