时间:2021-07-01 10:21:17 帮助过:18人阅读
System.out.println("数据库连接成功");
} catch (SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
}
}
数据库查询
package com.Aha.Best;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JAVASelectMSS {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败");
e.printStackTrace();
}
try {
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=MyDataBase","sa","zl225226");
System.out.println("数据库连接成功");
String sql = "select * from ADR_PROV";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
int id = rs.getInt("PROV_ID");
String name = rs.getString("Name"); ;
System.out.println("省号:"+id+" "+"名称:"+name);
}
if(rs != null){
rs.close();
System.out.println("查询结果完成");
}
if(ps != null){
ps.close();
System.out.println("关闭查询语句");
}
if(conn != null){
conn.close();
System.out.println("关闭数据库连接");
}
} catch (SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
}
}
数据库更新
package com.Aha.Best;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JAVAIUDMSS {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败");
e.printStackTrace();
}
try {
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=MyDataBase","sa","zl225226");
System.out.println("数据库连接成功");
Statement st = conn.createStatement();
//String sql = "insert into ADR_PROV(PROV_ID,name) values(930000,‘意大利‘);insert into ADR_PROV(PROV_ID,name) values(940000,‘俄罗斯‘);";
//String sql ="update ADR_PROV set PROV_ID = right(PROV_ID,5) where Prov_ID like ‘9%‘";
String sql = "delete ADR_prov where len(PROV_ID) = 5";
int num = st.executeUpdate(sql);
System.out.println("受影响的"+num+"行");
if(st != null){
st.close();
System.out.println("更新语句完成");
}
if(conn != null){
conn.close();
System.out.println("关闭数据库连接");
}
} catch (SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
}
}
连接数据库
package com.Aha.Best;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JAVAConnOracle {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败");
e.printStackTrace();
}
try {
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","sys as sysdba","orcl");
System.out.println("数据库连接成功");
} catch (SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySqlJDBC {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
String user = "root";
String password = "123456";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("驱动加载成功");
conn = DriverManager.getConnection(url, user, password);
System.out.println("连接数据库成功");
st = conn.createStatement();
System.out.println("进入状态");
String sql = "select * from student;";
rs = st.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));
}
if(rs != null){rs.close();}
if(st != null){st.close();}
if(conn != null){conn.close();}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
20JAVA数据库操作
标签:for 连接失败 组成 学生 use 用户 中间件 建立数据库 sql