ResultSet rs=s.executeQuery("select * from teacher"); //查询语句
创新互联建站是专业的宜兴网站建设公司,宜兴接单;提供网站制作、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行宜兴网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
s.executeUpdate("update into teacher set ....."); //插入,更新 ,删除语句
/**
*插入,更新 ,删除
*主要就是SQL语句不同
*/
增删改查都有链接网页链接
粘贴着查询的:
package cn.web.jdbc;
import java.sql.*;
public class UserLogin {
public static void main(String[] args) {
// 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
// 获取连接
String url = "jdbc:mysql://localhost:3306/usejdbc?useUnicode=truecharacterEncoding=UTF-8useSSL=false";
String user = "root";
String mysqlPassword = "123456";
//模拟前台传入的用户名和密码
String InputUsername = "老八";
String InputPassword = "123456";
try {
// 连接对象输入三个参数
Connection connection = DriverManager.getConnection(url, user, mysqlPassword);
System.out.println(connection);
//定义sql语句
// 查询
String sql1 = "select * from student where username='" + InputUsername + "' and password='" + InputPassword + "'";
System.out.println(sql1);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql1);
System.out.println(resultSet);
if (resultSet.next()) {
System.out.println("登录成功");
} else {
System.out.println("登录失败");
}
// 释放资源
statement.close();
connection.close();
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
对数据库进行增删改查?
以下是 sql server 2013+java.实现的是对MSC对象的增删改查.
需要下载连接驱动程序:com.microsoft.sqlserver.jdbc.SQLServerDriver
网上搜一下就行
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class MSC
{
public String MscID;
public String MscName;
public String MscCompany;
public float MscLongitude;
public float MscLatitude;
public float MscAltitude;
public MSC(String MscID, String MscName, String MscCompany,
float MscLongitude, float MscLatitude,float MscAltitude){
this.MscID = MscID;
this.MscName = MscName;
this.MscCompany = MscCompany;
this.MscLongitude =MscLongitude;
this.MscLatitude = MscLatitude;
this.MscAltitude = MscAltitude;
}
}
public class sqlserverjdbc {
public Connection getConnection(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=gsm"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "123"; //密码
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn =DriverManager.getConnection(dbURL, userName, userPwd);
} catch (Exception e) {
e.printStackTrace();
}
return dbConn;
}
public void printUserInfo(){
Connection con = getConnection();
Statement sta = null;
ResultSet rs = null;
System.out.println("打印表格MSC信息");
try {
sta = con.createStatement();
rs = sta.executeQuery("select * from MSC信息");
System.out.println("MscID\tMscName\tMscCompany\tMscLongitude\tMscLatitude\tMscAltitude");
while(rs.next()){
System.out.println(rs.getString("MscID")+"\t"+
rs.getString("MscName")+"\t"+
rs.getString("MscCompany")+"\t"+
rs.getFloat("MscLongitude")+"\t"+
rs.getFloat("MscLatitude")+"\t"+
rs.getFloat("MscAltitude"));
}
con.close();
sta.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("打印完成\n");
}
public void delete(String MscID){
Connection con = getConnection();
String sql = "delete from MSC信息 where MscID = " + MscID;
PreparedStatement pst;
System.out.println("删除表格MSC信息中 ID = "+MscID+"的记录");
try {
pst = con.prepareStatement(sql);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("记录删除失败!!!");
}
System.out.println("记录删除成功!!!\n");
}
public void insert(MSC msc){
Connection con = getConnection();
String sql = "insert into MSC信息 values(?,?,?,?,?,?)";
PreparedStatement pst;
System.out.println("插入一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, msc.MscID);
pst.setString(2, msc.MscName);
pst.setString(3, msc.MscCompany);
pst.setFloat(4, msc.MscLongitude);
pst.setFloat(5, msc.MscLatitude);
pst.setFloat(6, msc.MscAltitude);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失败!!!");
}
System.out.println("插入成功!!!\n");
}
//更新MscID的MscName
public void updateMscName(String MscID, String MscName){
Connection con = getConnection();
String sql = "update MSC信息 set MscName = ? where MscID = ?";
PreparedStatement pst;
System.out.println("修改一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, MscName);
pst.setString(2, MscID);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改失败!!!");
}
System.out.println("修改成功!!!\n");
}
public static void main(String args[]){
sqlserverjdbc sql = new sqlserverjdbc();
sql.printUserInfo();
sql.delete("1111");
sql.printUserInfo();
sql.updateMscName("5215", "联想");
sql.printUserInfo();
sql.insert(new MSC("1111", "中兴" ," 中兴", (float)12.2, (float)3.4,(float)45.5));
sql.printUserInfo();
}
}
首先你得确定你的数据库连接是通过什么形式连接的,hibernate还是原生态的jdbc 还是spring;
如果是只有hibernate,那么你得通过加载配置文件得到sessionFactory,然后得到session
如果spring,那么同样也需要注入sessionfactory到你的dao
如果是jdbc方式,那么你就按照原生态jdbc写法
总之,在你构造DAO时,得有数据源。这样才能操纵你的数据库
如果搞懂了这些问题,那么你的第一个,第三个问题就迎刃而解了。至于第二问题,我没明白你什么意思!