本文目录一览:
怎样在java程序中执行 SQL 语句
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// System.out.println("XOX");
con = DriverManager.getConnection(connectionUrl);
// System.out.println("XOX..");
String Uinfo = "SELECT UserName,UserPwd FROM Login";
// System.out.println("XXX");
stmt = con.createStatement();
// System.out.println("000");
rs1 = stmt.executeQuery(Uinfo);
你都写到这了,下面就照着我说的写呗。
一条记录就
rs1.next();
Uname = rs1.getString(0);
Upwd = rs1.getString(1);
多条就
while(rs1.next()) {
Uname = rs1.getString(0);
Upwd = rs1.getString(1);
} 我说的很明白了吧。
如何用java语言中如何执行sql查询语句
使用jdbc技术,首先导入连接数据库的jar包,声明加载的类名,获取连接,进行查询,如:
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
Statement sta = conn.createStatement();
ResultSet rs = sta.executeQuery("select * from tablename");
java如何执行sql语句
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class xxxx {
public static void main(String[] args) {
Connection con = null ;
Statement stmt = null ;
try {
Class.forName("com.mysql.jdbc.Driver"); //mysql为例 不一样的数据库所需的驱动包不一样 连接语句略有不同
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/数据库名", "root", "密码");
stmt = con.createStatement();
String sql = "insert into info values ('用户', 'mima', 'piapiapia~')";
stmt.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}