mysql语句转java语句
时间: 2023-04-07 21:05:26 浏览: 103
可以使用Java JDBC API来执行MySQL语句。以下是一个示例:
```
import java.sql.*;
public class MySQLtoJava {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "password");
stmt = conn.createStatement();
String sql = "SELECT * FROM users";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
rs.close();
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt!=null)
conn.close();
} catch(SQLException se) {
}
try {
if(conn!=null)
conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
}
}
```
这个示例使用了MySQL的JDBC驱动程序,连接到本地MySQL数据库,执行一个SELECT语句,并将结果打印到控制台。你可以根据你的需求修改这个示例。
阅读全文
相关推荐














