java数据库查找代码,JAVA查询数据库
java链接MYSQL数据库并实现查找数据代码如何写?
首先创建一个连接工厂import java.sql.Connection;
天宁网站建设公司创新互联,天宁网站设计制作,有大型网站制作公司丰富经验。已为天宁千余家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的天宁做网站的公司定做!
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;public class ConnectionFactory {
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
public ConnectionFactory() {
super();
// TODO Auto-generated constructor stub
} public void OpenConn() throws Exception{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://127.0.0.1:3306/guestbook";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
}catch(Exception e){
System.out.println("创建链接抛出异常为:"+e.getMessage());
}
} public ResultSet executeQuery(String sql) throws Exception{
try{
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}catch(Exception e){
System.out.println("执行查询抛出的异常为:"+e.getMessage());
}
return rs;
} public void close() throws Exception{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println("关闭对象抛出的异常:"+e.getMessage());
}
} }
测试类 import java.sql.ResultSet;public class TestJDBC {
public static void main(String[] args) {
ConnectionFactory c= new ConnectionFactory();
try {
c.OpenConn();
String sql="select * from tb_guestbook";
ResultSet rs=c.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(2));
}
c.close();
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
JAVA中怎么查询代码?
try{Connection con;
Statement stmt;
ResultSet rs;
int temp;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","");//以上是数据库连接,不同的数据管理器有 //不同的驱动和链接方式,以上是mysql的连接
stmt=con.createStatement();
rs=stmt.executeQuery("select * from student");//执行查询语句,结果赋值给结果集rs
//结果集是结果于字段编号的映射,每一个字
//段都有一个编号,最小为1,也就是第一个字段
while(rs.next()){
String names=rs.getString("name");//查询结果转换成字符串。
System.out.println(names);
}rs.close();
}catch(Exception e){
e.printStackTrace();
}
如何用Java实现数据库查询
import java.sql.*;
public class MSSQLText
{
public static void main(String args[])
{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";
String user="sa";//这里替换成你自已的数据库用户名
String password="sa";//这里替换成你自已的数据库用户密码
String sqlStr="select CustomerID, CompanyName, ContactName from Customers";
try
{ //这里的异常处理语句是必需的.否则不能通过编译!
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("类实例化成功!");
Connection con = DriverManager.getConnection(url,user,password);
System.out.println("创建连接对像成功!");
Statement st = con.createStatement();
System.out.println("创建Statement成功!");
ResultSet rs = st.executeQuery(sqlStr);
System.out.println("操作数据表成功!");
System.out.println("----------------!");
while(rs.next())
{
System.out.print(rs.getString("CustomerID") + " ");
System.out.print(rs.getString("CompanyName") + " ");
System.out.println(rs.getString("ContactName"));
}
rs.close();
st.close();
con.close();
}
catch(Exception err){
err.printStackTrace(System.out);
}
}
}
java连接数据库的代码
package mysql;
import java.sql.*;
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("数据库连接成功");
} else {
System.out.println("数据库连接失败");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 实例化 Statement 对象
Statement statement = getConnection().createStatement();
// 要执行的 Mysql 数据库操作语句(增、删、改、查)
String sql = "";
// 展开结果集数据库
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通过字段检索
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 输出数据
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
// 完成后需要依次关闭
resultSet.close();
statement.close();
getConnection().close();
}
}
文章标题:java数据库查找代码,JAVA查询数据库
网页链接:http://pwwzsj.com/article/hcgcei.html