java程序连接sql server2008数据库:求给一个具体的代码,和过程。
展开全部
连接数据库
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDB {
public static void main(String[] args) {
try {
// 1.注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("注册驱动成功");
String url = ""jdbc:microsoft.sqlserver://127.0.0.1:1433;DatabaseName="; //数据库IP,端口,数据库名
String username = "";
String password = "";
// 加载驱动并获取数据库连接对象
Connection connection = DriverManager.getConnection(url, username,
password);
System.out.println("连接数据库成功!");
// 创建Statement对象
Statement stmt = connection.createStatement();
// SQL语句
String sql = "insert into student(id,name,age,email,address) values(1,'tom',23,'dfg@163.com','England') ";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("插入成功!");
}
// 释放资源
stmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
预编译的数据库处理
public class ConnectionManager{
public static final String DRIVER_CLASS ="";
public static final String URL = "";
public static final String USERNAME = "";
public static final String PASSWORD = "";
//获取连接对象
public static Connection getConnection(){
Connection conn = null;
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
try{
if(rs!=null){
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}
if(conn!=null){
conn.close();
conn=null;
}
}
}
}
public class DBOperator{
public int executeUpdate(String sql,Object[] params){
int rows = 0;
Connection connn = ConnectionManager.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1,params[i]);//下标从1开始
}
}
rows = pstmt.executeUpdate();
ConnectionManager.closeAll(null,pstmt,conn);
return rows;
}
public ResultSet executeQuery(String sql,Object[] params){
Connetion conn = ConnectionManager.getConnection();
if(params!=null){
for(int i= 0;i<params.length;i++){
pstmt.setObject(i+1,params(i));
}
}
rs=pstmt.executeQuery();
ConnectionManager.closeAll(rs,pstmt,conn);
return rs;
}
}
/**
*statement连接数据库
*/
public class TestOperatorGrade{
private static void updateGrade(int id,String name,String description){
Class.forName();//注册驱动
Connection conn = DirverManager.getConnection("","","");
Statement stmt = conn.createStament();
String sql = "UPDATE grades set name = '"+name+"',description='"+description+"'WHere id="+id;
int rows = stmt.executeUpdate(sql);
}
}
这是java 程序的代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDB {
public static void main(String[] args) {
try {
// 1.注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("注册驱动成功");
String url = ""jdbc:microsoft.sqlserver://127.0.0.1:1433;DatabaseName="; //数据库IP,端口,数据库名
String username = "";
String password = "";
// 加载驱动并获取数据库连接对象
Connection connection = DriverManager.getConnection(url, username,
password);
System.out.println("连接数据库成功!");
// 创建Statement对象
Statement stmt = connection.createStatement();
// SQL语句
String sql = "insert into student(id,name,age,email,address) values(1,'tom',23,'dfg@163.com','England') ";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("插入成功!");
}
// 释放资源
stmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
预编译的数据库处理
public class ConnectionManager{
public static final String DRIVER_CLASS ="";
public static final String URL = "";
public static final String USERNAME = "";
public static final String PASSWORD = "";
//获取连接对象
public static Connection getConnection(){
Connection conn = null;
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
try{
if(rs!=null){
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}
if(conn!=null){
conn.close();
conn=null;
}
}
}
}
public class DBOperator{
public int executeUpdate(String sql,Object[] params){
int rows = 0;
Connection connn = ConnectionManager.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1,params[i]);//下标从1开始
}
}
rows = pstmt.executeUpdate();
ConnectionManager.closeAll(null,pstmt,conn);
return rows;
}
public ResultSet executeQuery(String sql,Object[] params){
Connetion conn = ConnectionManager.getConnection();
if(params!=null){
for(int i= 0;i<params.length;i++){
pstmt.setObject(i+1,params(i));
}
}
rs=pstmt.executeQuery();
ConnectionManager.closeAll(rs,pstmt,conn);
return rs;
}
}
/**
*statement连接数据库
*/
public class TestOperatorGrade{
private static void updateGrade(int id,String name,String description){
Class.forName();//注册驱动
Connection conn = DirverManager.getConnection("","","");
Statement stmt = conn.createStament();
String sql = "UPDATE grades set name = '"+name+"',description='"+description+"'WHere id="+id;
int rows = stmt.executeUpdate(sql);
}
}
这是java 程序的代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//插入
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL鏁版嵁搴揿紩鎿�
String connectDB="jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest";//鏁版嵁婧惵�
String user="sa";
String pwd="sql";
try {
// 动态导入数据库的驱动
Class.forName(JDriver);
// 获取数据库链接
conn = DriverManager.getConnection(connectDB,user,pwd);
// 创造SQL语句
String sql = "INSERT INTO JDBCTestTable ( user_name, user_password ) VALUES ( 'balabala', '123456' )";
// 执行SQL语句
stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("插入数据成功");
} catch (Exception e) {
e.printStackTrace();
stmt.close();
conn.close();
}
}
//查询
public static void main(String[] args){
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest";
String user="sa";
String password="sql";
try{
Class.forName(JDriver);// 动态导入数据库的驱动
Connection conn=DriverManager.getConnection(url, user, password);// 获取数据库链接
String query="SELECT * FROM JDBCTestTable";// 创造SQL语句
Statement stmt=conn.createStatement();// 执行SQL语句
ResultSet rs=stmt.executeQuery(query);
while(rs.next()){
System.out.println(rs.getString("user_name")+":"+rs.getString(2));
//密码字段的编号从1开始,密码排第二位
}
System.out.println("查询数据成功");
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
//删除
public static void main(String[] args){
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest";
String user="sa";
String password="sqlyanghai";
try{
Class.forName(JDriver);// 动态导入数据库的驱动
Connection conn=DriverManager.getConnection(url, user, password);// 获取数据库链接
String query="DELETE FROM JDBCTestTable WHERE user_name='Eric'";// 创造SQL语句
Statement stmt=conn.createStatement();// 执行SQL语句
stmt.executeUpdate(query);
System.out.println("删除数据成功");
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
//修改
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
// 动态导入数据库的驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 获取数据库链接
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest",
"sa", "sql");
// 创造SQL语句
String sql = "UPDATE JDBCTestTable SET user_password = '456' WHERE user_name = 'Eric'";
// 执行SQL语句
stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("更新数据成功");
} catch (Exception e) {
e.printStackTrace();
stmt.close();
conn.close();
}
}
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL鏁版嵁搴揿紩鎿�
String connectDB="jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest";//鏁版嵁婧惵�
String user="sa";
String pwd="sql";
try {
// 动态导入数据库的驱动
Class.forName(JDriver);
// 获取数据库链接
conn = DriverManager.getConnection(connectDB,user,pwd);
// 创造SQL语句
String sql = "INSERT INTO JDBCTestTable ( user_name, user_password ) VALUES ( 'balabala', '123456' )";
// 执行SQL语句
stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("插入数据成功");
} catch (Exception e) {
e.printStackTrace();
stmt.close();
conn.close();
}
}
//查询
public static void main(String[] args){
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest";
String user="sa";
String password="sql";
try{
Class.forName(JDriver);// 动态导入数据库的驱动
Connection conn=DriverManager.getConnection(url, user, password);// 获取数据库链接
String query="SELECT * FROM JDBCTestTable";// 创造SQL语句
Statement stmt=conn.createStatement();// 执行SQL语句
ResultSet rs=stmt.executeQuery(query);
while(rs.next()){
System.out.println(rs.getString("user_name")+":"+rs.getString(2));
//密码字段的编号从1开始,密码排第二位
}
System.out.println("查询数据成功");
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
//删除
public static void main(String[] args){
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest";
String user="sa";
String password="sqlyanghai";
try{
Class.forName(JDriver);// 动态导入数据库的驱动
Connection conn=DriverManager.getConnection(url, user, password);// 获取数据库链接
String query="DELETE FROM JDBCTestTable WHERE user_name='Eric'";// 创造SQL语句
Statement stmt=conn.createStatement();// 执行SQL语句
stmt.executeUpdate(query);
System.out.println("删除数据成功");
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
//修改
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
// 动态导入数据库的驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 获取数据库链接
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest",
"sa", "sql");
// 创造SQL语句
String sql = "UPDATE JDBCTestTable SET user_password = '456' WHERE user_name = 'Eric'";
// 执行SQL语句
stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("更新数据成功");
} catch (Exception e) {
e.printStackTrace();
stmt.close();
conn.close();
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询