package jw;
import java.sql.*;
public class Jw {
public static void main(String[] args) {
String url = "jdbc:oracle:thin:@host:1521:test1"; //test为自己数据库的名字,1521为端口
String username = "user";
String password = "pass";
String driver = "oracle.jdbc.driver.OracleDriver";
try {
Class.forName(driver); //加载驱动
Connection conn = DriverManager.getConnection(url, username,
password); //连接数据库
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM JW.XJ_XJB"); // 预处理命令,比Statement 好一些
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(1));
}
pstmt.close(); //关闭连接
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
String myurl = "jdbc:mysql://localhost:3306/test";
String user = "user";
String pwd = "pass";
Connection connection = null;
String sql = null;
Statement stmt = null;
//ResultSet rs = null;
try {
connection = DriverManager
.getConnection(myurl,user,pwd);
stmt = connection.createStatement();
//我要在这里获取上面ResultSet rs = pstmt.executeQuery(); 的rs.getString(1)数据应该如何获取?
sql = "INSERT INTO XJ_XJB(XH,XM) VALUES($1,$2)";
int rs = stmt.executeUpdate(sql);
System.out.println("mysql ok");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
//System.out.println(tmp);
} else {
System.out.println("Failed to make connection!");
}
}
}
关于数据库查询值的调用问题
最新推荐文章于 2021-01-19 16:45:11 发布